简体   繁体   中英

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.

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.

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

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). 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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