Setting association to foreign key to inherited object using graphdiff and Entity Framework

c# ef-code-first entity-framework graphdiff

Question

I'm having trouble updating a value on a foreign key that is descended from a different class. I'm using graphdiff to manage disconnected objects that I'm working with in Entity Framework.

To make the code easier to read, I've simplified it.

Project type:

public class Project
{
    public Guid Id { get; set; }

    public virtual List<Activity> Activities { get; set; }
}

Activity type

public class Activity
{
    public Guid Id { get; set; }

    public String Name { get; set; }

    public virtual Company Company { get; set; }
}

Activity class: surfing

public class SurfingActivity : Activity
{
    public String Comment { get; set; }

    public virtual Weather Weather { get; set; }
}

The graphdiff update context is as follows:

public void UpdateActivity(Project project)
{
    this.UpdateGraph(project, map => map
        .OwnedCollection(p => p.Activities, with => with
            .AssociatedEntity(a => a.Company)
        )
    );
}

I can only link the Company property to an activity; I cannot link the weather to a surfing activity. Id, Name, Company, and Comment are recorded when I pass values for SurfingActivity along, but weather is not preserved in either the Activity table or the SurfingActivity table. Any ideas on how to resolve this issue without having to add a new property to the project that contains a list with the surfingActivity are welcome.

1
1
5/4/2017 9:33:55 AM

Accepted Answer

According to Andy Pelzer of GraphDiff, this is not currently supported. The workaround I propose would be to add the foreign key property as the datatype it is and data annotation ForeignKey to the foreign key with the entity, as in the following example:

    public System.Guid? Weather_Id { get; set; }

    [ForeignKey("Weather_Id")]
    public virtual Weather Weather { get; set; }

Referring to: https://github.com/refactorthis/GraphDiff/issues/112

1
11/4/2014 9:56:23 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow