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

c# ef-code-first entity-framework graphdiff

Question

I have a issue with updating value on a foreign key which is inheriting from another class. I am working with detached object in Entity Framework, so I'm using graphdiff to handle it.

I've simplified the code to make it easier to read

Project class:

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

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

Activity class:

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

    public String Name { get; set; }

    public virtual Company Company { get; set; }
}

SurfingActivity class:

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

    public virtual Weather Weather { get; set; }
}

The update context using graphdiff:

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

I'm only able to associate the Company property in Activity but not the Weather of SurfingActivity. When I pass along values for SurfingActivity does Id, Name, Company and Comment get saved in the Activity table and the SurfingActivity table but not weather. Does anyone have any suggestion how to solve this without having to create a new property on Project that contains a list with surfingActivity

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

Accepted Answer

I asked andypelzer in GraphDiff and this is currently not supported. My work around, which would work in this case (but not if the entities becomes even more nested) is to add the foreign key property as datatype it is and data annotation ForeignKey to the foreign key with the entity, like this:

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

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

Link to question: 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