Unable to Determine Principal End - Entity Framework Code First Relationship

asp.net c# ef-code-first entity-framework

Question

PLEASE READ THE SECTION BELOW ON POSSIBLE SOLUTIONS.

Here are my tables, and I have a problem with a foreign key relationship.

public class Lead
{
    [Key]
    public int LeadId { get; set; }
    public int? CustomerId { get; set; }

    [ForeignKey("CustomerId")]
    public virtual Customer Customer { get; set; }
 }

public class Customer
{
    [Key]
    [Column("customer_id")]
    public int CustomerId { get; set; }

    [ForeignKey("CustomerId")]
    public virtual Lead Lead { get; set; }

}

I am experiencing a problem with this error:

Unable to determine the principal end of an association between the types 'Sales.Customers.Customer' and 'Sales.Leads.Lead'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.

Although I tried adding the relationship to the modelbuilder, it doesn't seem to be functioning properly. It really uses the when I do get the error message to disappearLead.LeadId -> Customer.CustomerId as opposed to the relationshipLead.CustomerId -> Customer.CustomerId relationship.

I reviewed related Stackoverflow questions, but they don't appear to match my database structure, and when I try to use their recommendations the relationship still doesn't work correctly.

It's pretty strange, and I really need some assistance.

UPDATE

I have therefore flipped the keys in an effort to make this relationship work as follows:

public class Lead
{
    [Key]
    public int LeadId { get; set; }

    [ForeignKey("LeadId")]
    public virtual Customer Customer { get; set; }
}

public class Customer
{
    [Key]
    [Column("customer_id")]
    public int CustomerId { get; set; }
    public int? LeadId { get; set; }

    [ForeignKey("LeadId")]
    public virtual Lead Lead { get; set; }
}

But same mistake, same result – I have no idea why this relationship won't work. It looks quite simple to me.

REVISED 2

Okay, after wasting a ton of time tinkering with this, I tried a slightly different strategy:

Here are my upcoming classes.

    public class Lead
    {
        [Key, ForeignKey("Customer")]
        public int LeadId { get; set; }
        public virtual Customer Customer { get; set; }
    }

    public class Customer
    {
        [Key]
        [Column("customer_id")]
        public int CustomerId { get; set; }
        public int? LeadId { get; set; }
        public virtual Lead Lead { get; set; }
     }

With the aforementioned code, no more error warnings! The Customer is the only issue that the relationship entity framework is causing. Lead and CustomerId. instead of the Customer, use LeadId. Lead and LeadId. I feel like I'm SO CLOSE to getting LeadId!

POSITIVE RESULTS

Okay, so I found this post here after doing some more research: 1-to-1 Optional Relationship with EF Code First

I changed my classes to be like this:

public class Customer
{
    [Key]
    [Column("customer_id")]
    public int CustomerId { get; set; }

    public virtual Lead Lead { get; set; }
}

public class Lead
{
    [Key]
    public int LeadId { get; set; }
    public virtual Customer Customer { get; set; }
 }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Customer>().HasOptional<Lead>(l => l.Lead).WithOptionalDependent(c => c.Customer).Map(p => p.MapKey("LeadId"));
    base.OnModelCreating(modelBuilder);
}

It all works great! But there's a HUGE issue...

Since I had to delete the LeadId property from the Customer Table, how am I supposed to assign a LeadId when creating a new Customer (when appropriate) if there isn't a LeadId column to assign to?

1
5
5/23/2017 10:27:49 AM

Popular Answer

It should function if I post this in the Fluent API.

public class Lead
{
    [Key]
    public int LeadId { get; set; }

    public virtual Customer Customer { get; set; }
 }

public class Customer
{
    [Key]
    [Column("customer_id")]
    public int CustomerId { get; set; }

    public virtual Lead Lead { get; set; }
}

builder.Entity<Lead>()
.HasOptional(l => l.Customer)
.WithOptionalPrincipal()
.Map(k => k.MapKey("LeadId"));

builder.Entity<Customer>()
.HasOptional(c => c.Lead)
.WithOptionalPrincipal()
.Map(k => k.MapKey("CustomerId"));

EDIT

Tables

2
7/16/2013 2:23:33 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow