c# - 无法确定主体端 - Entity Framework 代码优先关系

标签 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 上检查过类似的问题,但它们似乎与我的数据库结构不匹配,当我尝试实现他们的建议时,关系仍然无法正常工作。

这真的很奇怪 - 非常感谢对此的帮助!

更新

因此,在我试图让这种关系发挥作用的过程中,我通过以下方式切换了 key :

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

好的 - 在浪费了大量时间之后,我尝试了一种稍微不同的方法:

这是我的新类(class)....

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

上面的代码没有更多的错误消息!唯一的问题是关系 Entity Framework 正在 Customer.CustomerId 和 Lead.LeadId 之间创建,而不是在 Customer.LeadId 和 Lead.LeadId 之间创建 - 我觉得我太接近了!

可能的解决方案

好的 - 经过更多研究后,我在这里看到了这篇文章: EF Code First - 1-to-1 Optional Relationship

我将我的类(class)修改为:

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

一切正常!但是有一个大问题……

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

最佳答案

用流畅的 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

关于c# - 无法确定主体端 - Entity Framework 代码优先关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17658619/

相关文章:

c# - 获取没有完整命名空间的类型名称

c# - 生成随机整数时无法包含上限

c# - 如何计算从今天算起的8个工作日?

asp.net - 二级缓存不缓存NHibernate中过滤的集合?

javascript - 我如何验证某人从出生之日起已满 18 周岁?

c# - 如何通过分组将 <Entities> 列表转换为一个字符串?

c# - C# for 循环中的两个计数器

c# - .NET Core 中的 System.Attribute.GetCustomAttribute

c# - 接受过滤条件和要过滤的属性的通用 Linq to Entities 过滤方法

c# - Entity Framework 计数效率