无法确定主要终端 - 实体框架代码第一关系

时间:2013-07-15 16:00:57

标签: c# asp.net entity-framework ef-code-first

请检查以下可能的解决方案

我遇到外键关系问题 - 这是我的表格:

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; }

}

我遇到了一个问题,我收到此错误:

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.

我尝试将关系添加到模型构建器,但看起来它无法正常工作。当我收到错误消息时,实际上使用Lead.LeadId -> Customer.CustomerId作为关系而不是Lead.CustomerId -> Customer.CustomerId关系。

我在Stackoverflow上检查了类似的问题,但它们似乎与我的数据库结构不匹配,当我尝试实现他们的建议时,关系仍然无法正常工作。

真的很奇怪 - 非常感谢这方面的帮助!

更新

因此,在尝试让这种关系发挥作用时,我已按以下方式切换键:

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; }
}

然而,同样的错误,仍然没有运气 - 我真的不知道为什么这种关系不起作用。对我而言,这似乎很简单。

更新2

好的 - 经过一段时间的浪费,我尝试了一种稍微不同的方法:

以下是我的新课程......

    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; }
     }

上面的代码不再有错误消息!唯一的问题是关系实体框架是在Customer.CustomerId和Lead.LeadId之间创建的,而不是Customer.LeadId和Lead.LeadId - 我觉得我很紧张!

可能的解决方案

好的 - 经过一些研究之后我在这里发现了这篇文章: EF Code First - 1-to-1 Optional Relationship

我修改了我的课程:

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);
}

一切都很棒!但是一个大问题......

我不得不从客户表中删除LeadId属性....所以现在我不确定如果没有要分配的LeadId属性,在创建新客户(适当时)时如何分配LeadId?

1 个答案:

答案 0 :(得分:2)

在流畅的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"));

<强> 修改

Tables