EF Core 2.0 - 导航属性只能参与单个关系

时间:2018-01-30 11:32:10

标签: c# entity-framework asp.net-core .net-core

我们说我有这些模型:

public class Component
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Type { get; set; }
    public List<ComponentUpdate> Updates { get; set; }
    public ComponentUpdate LastUpdate { get; set; }
}

public class ComponentUpdate
{
    public int Id { get; set; }
    public DateTime Timestamp { get; set; }
    public Component Component { get; set; }
    public string Message { get; set; }
}

我保存LastUpdate字段而不是根据最高&#39; TimeStamp&#39;手动拉取它的原因。是因为速度。存储引用而不是每次请求都检查整个列表会更快。

当我尝试迁移数据库时,它会抛出一条错误消息,说我不能让我的属性参与多个关系。 我在我的上下文类中映射关系,我不认为我正确地执行它,因为我将ComponentUpdate.Component映射了两次。

我已经看了几个解决方案,但有些已经过时,有些只是不符合这种情况。

感谢您的帮助。

修改

相应的映射:

modelBuilder.Entity<Component>().HasMany(c => c.Updates).WithOne(u => u.Component);
modelBuilder.Entity<ComponentUpdate>().HasOne(u => u.Component).WithOne(c => c.LastUpdate);

0 个答案:

没有答案