简体   繁体   English

实体框架ForeignKey麻烦

[英]Entity Framework ForeignKey troubles

First off I would like to just state that I do see this question A LOT. 首先,我想说的是我确实看到了这个问题很多。 I understand that there may be a duplicate, but I have searched and searched and I haven't found the right solution. 我知道可能有重复,但我搜索和搜索,我没有找到正确的解决方案。

 public class Members
{
    public enum Statuses
    {
        APPLIED,
        ACTIVE,
        SUSPENDED,
        DELETED
    }

    [Key]
    public int ID { get; set; }
    [Required]
    public string UName { get; set; }
    public int RecruiterID { get; set; }
    public int AuditorID { get; set; }
    public virtual ICollection<AuditorComments> AuditorComments { get; set; }
    [Required]
    public Statuses Status { get; set; }
    [Timestamp]
    public Byte[] Timestamp { get; set; }

    [ForeignKey("RecruiterID")]
    public virtual Members Recruiter { get; set; }
    [ForeignKey("AuditorID")]
    public virtual Members Auditor { get; set; }
}

Basically am I tying the foreign keys together right? 我基本上把外键绑在一起吧?

Here is the error I am receiving: 这是我收到的错误:

Unable to determine the principal end of an association between the types
'tn.Data.Members' and 'tn.Data.Members'. The principal end of this association
must be explicitly configured using either the relationship fluent API or data
annotations.

I have MANY other tables like this, but if I could just get this one to work then I would be able to fix them all. 我有很多像这样的其他表,但如果我能让这一个工作,那么我将能够解决所有问题。

I would conciser using fluent specification of these relationships as they are a bit tidier, leave the actual classes pure POCO and IMO are easier to read. 我会使用这些关系的流畅规范来说明,因为它们有点整洁,让实际的类纯POCO和IMO更容易阅读。

On another note the structure you are describing is actually not really possible with SQL as Members have required Members, this means you cant bootstrap your model and it will always have loops in it. 另外请注意,您所描述的结构实际上并不可能使用SQL,因为成员需要成员,这意味着您无法引导模型,并且它将始终具有循环。

Here is how you could do this with fluent configuration. 以下是如何使用流畅的配置来完成此操作。

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Members>()
        .HasOptional(m => m.Auditor)
        .WithMany()
        .HasForeignKey(p => p.AuditorId);

    modelBuilder.Entity<Members>()
        .HasOptional(m => m.Recruiter)
        .WithMany()
        .HasForeignKey(p => p.RecruiterId);
}

for some more details on how to use navigation properties with EF check out my article here: http://blog.staticvoid.co.nz/2012/7/17/entity_framework-navigation_property_basics_with_code_first 有关如何使用EF导航属性的更多详细信息,请查看我的文章: http//blog.staticvoid.co.nz/2012/7/17/entity_framework-navigation_property_basics_with_code_first

Just to add to Luke McGregor's answer, this is happening because you have two self-referential foreign keys in the same table, and by default the Entity Framework will jump to the wrong conclusion and think that means they're opposite ends of the same relationship (ie, it's wrongly assuming your two foreign keys are trying to establish a parent/child relationship). 只是为了补充Luke McGregor的答案,这种情况正在发生,因为你在同一个表中有两个自引用外键,默认情况下,实体框架会跳到错误的结论并认为这意味着它们是同一关系的两端(即,假设您的两个外键正在尝试建立父/子关系,这是错误的)。 That's why it's asking which one is the principal and which one is the dependent. 这就是为什么它要问哪一个是主体,哪一个是依赖。

Right now, I don't think there's a way to correct its misunderstanding with data annotation attributes alone, so you'll have to use the Fluent API as Luke suggests. 现在,我认为没有办法单独使用数据注释属性来纠正其误解,所以你必须像Luke建议的那样使用Fluent API。

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

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