必须使用关系流畅 API 或数据注释显式配置此关联的主体端 [英] The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations

查看:32
本文介绍了必须使用关系流畅 API 或数据注释显式配置此关联的主体端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

必须使用关系流畅 API 或数据注释显式配置此关联的主体端."

更新/迁移数据库时,我在 Entity Framework 4.4 中收到此错误,但我并未尝试指定 1:1 关系.我想要这样的东西:

I am getting this error in Entity Framework 4.4 when updating/migrating the database, but I am not trying to specify a 1:1 relationship. I want something like this:

public class EntityA
{
    public int ID { get; set; }
    public int EntityBID { get; set; }

    [ForeignKey("EntityBID")]
    public virtual EntityB EntityB { get; set; }
}

public class EntityB
{
    public int ID { get; set; }
    public Nullable<int> PreferredEntityAID { get; set; }

    [ForeignKey("PreferredEntityAID")]
    public virtual EntityA PreferredEntityA { get; set; }
}

其中 EntityA 必须有一个 EntityB 父级,而 EntityB 可以有一个首选的 EntityA 子级,但不是必须的.首选子项应该是与父项关联的子项之一,但我不知道如何在数据库中强制执行此操作.我计划以编程方式强制执行它.

where EntityA must have an EntityB parent, whereas EntityB can have a preferred EntityA child, but doesn't have to. The preferred child should be one of the children associated with the parent, but I don't know how to enforce this in the database. I'm planning on enforcing it programmatically.

我如何解决这个错误或者有什么更好的方法来完成这些关系?

How do I get around this error or what is a better way of accomplishing these relationships?

推荐答案

Entity Framework Code-First 约定假设 EntityA.EntityBEntityB.PreferredEntityA 属于相同的关系并且是彼此的逆导航属性.因为两个导航属性都是引用(不是集合),所以 EF 推断出一对一的关系.

Entity Framework Code-First conventions are assuming that EntityA.EntityB and EntityB.PreferredEntityA belong to the same relationship and are the inverse navigation properties of each other. Because both navigation properties are references (not collections) EF infers a one-to-one relationship.

由于您实际上想要两个一对多关系,因此您必须覆盖约定.对于您的模型,只有使用 Fluent API 才能实现:

Since you actually want two one-to-many relationships you must override the conventions. With your model it's only possible with Fluent API:

modelBuilder.Entity<EntityA>()
    .HasRequired(a => a.EntityB)
    .WithMany()
    .HasForeignKey(a => a.EntityBID);

modelBuilder.Entity<EntityB>()
    .HasOptional(b => b.PreferredEntityA)
    .WithMany()
    .HasForeignKey(b => b.PreferredEntityAID);

(如果您使用它,您可以删除 [ForeignKey] 属性.)

(If you use this you can remove the [ForeignKey] attributes.)

您不能指定一个映射来确保首选子项始终是相关子项之一.

You cannot specify a mapping that would ensure that the preferred child is always one of the associated childs.

如果您不想使用 Fluent API 而只想使用数据注释,您可以在 EntityB 中添加一个集合属性,并使用 将其关联到 EntityA.EntityB>[InverseProperty] 属性:

If you don't want to use Fluent API but only data annotations you can add a collection property in EntityB and relate it to EntityA.EntityB using the [InverseProperty] attribute:

public class EntityB
{
    public int ID { get; set; }
    public Nullable<int> PreferredEntityAID { get; set; }

    [ForeignKey("PreferredEntityAID")]
    public virtual EntityA PreferredEntityA { get; set; }

    [InverseProperty("EntityB")] // <- Navigation property name in EntityA
    public virtual ICollection<EntityA> EntityAs { get; set; }
}

这篇关于必须使用关系流畅 API 或数据注释显式配置此关联的主体端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆