简体   繁体   English

使用graphdiff和Entity Framework将与外键的关联设置为继承对象

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

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. 我正在使用Entity Framework中的分离对象,因此正在使用graphdiff处理它。

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: SurfingActivity类:

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

    public virtual Weather Weather { get; set; }
}

The update context using graphdiff: 使用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. 当我传递SurfingActivity的值时,Id,Name,Company和Comment会保存在Activity表和SurfingActivity表中,但不会保存。 Does anyone have any suggestion how to solve this without having to create a new property on Project that contains a list with surfingActivity 没有人有任何建议,如何解决此问题,而不必在包含surfingActivity列表的Project上创建新属性

I asked andypelzer in GraphDiff and this is currently not supported. 我在GraphDiff中询问了andypelzer,目前不支持此功能。 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: 我的解决方法是在这种情况下起作用(但如果实体变得更加嵌套,则不行)是将外键属性添加为数据类型,并将数据注释ForeignKey添加到具有实体的外键,如下所示:

    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 链接到问题: https : //github.com/refactorthis/GraphDiff/issues/112

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM