【问题标题】:Setting association to foreign key to inherited object using graphdiff and Entity Framework使用 graphdiff 和实体框架设置与继承对象的外键的关联
【发布时间】:2014-11-03 10:13:53
【问题描述】:

我在更新从另一个类继承的外键上的值时遇到问题。我正在使用 Entity Framework 中的分离对象,所以我使用 graphdiff 来处理它。

我已简化代码以使其更易于阅读

项目类:

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

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

活动类:

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

    public String Name { get; set; }

    public virtual Company Company { get; set; }
}

SurfingActivity 类:

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

    public virtual Weather Weather { get; set; }
}

使用 graphdiff 的更新上下文:

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

我只能将 Company 属性关联到 Activity 中,但不能关联 SurfingActivity 的 Weather。当我传递 SurfingActivity 的值时,Id、Name、Company 和 Comment 是否会保存在 Activity 表和 SurfingActivity 表中,但不会保存在天气中。有没有人有任何建议如何解决这个问题而不必在 Project 上创建一个包含 surfingActivity 列表的新属性

【问题讨论】:

    标签: c# entity-framework ef-code-first graphdiff


    【解决方案1】:

    我在 GraphDiff 中询问了 andypelzer,目前不支持此功能。我的解决方法是,在这种情况下(但如果实体变得更加嵌套,则不行)是将外键属性作为数据类型添加,并将数据注释 ForeignKey 与实体的外键一起添加,如下所示:

        public System.Guid? Weather_Id { get; set; }
    
        [ForeignKey("Weather_Id")]
        public virtual Weather Weather { get; set; }
    

    问题链接:https://github.com/refactorthis/GraphDiff/issues/112

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-23
      相关资源
      最近更新 更多