无法确定主体终端 - 实体框架代码第一关系 [英] Unable to Determine Principal End - Entity Framework Code First Relationship

查看:187
本文介绍了无法确定主体终端 - 实体框架代码第一关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请检查可能的解决方案部分下面

我有一个外键关系的问题 - 这是我的表:

I have an issue with a foreign key relationship - here are my tables:

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'm having an issue where I receive 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.

我已经尝试将关系添加到模型构建器,但似乎它不能正常工作。当我得到错误消息,它实际上使用 Lead.LeadId - > Customer.CustomerId 作为关系,而不是 Lead.CustomerId - > Customer.CustomerId 关系。

I've tried adding the relationship to the modelbuilder but it appears that its not working properly. When I do get the error message to go away its actually using the Lead.LeadId -> Customer.CustomerId as the relationship instead of Lead.CustomerId -> Customer.CustomerId relationship.

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

I've checked similar questions on Stackoverflow but they don't seem to match my DB structure and when I try to implement their suggestions the relationship still doesn't work properly.

它真的很奇怪 - 非常感谢您的帮助!

Its really weird - would greatly appreciate help on this!

更新

所以在我尝试让这种关系工作的时候,我用下面的方式切换了这些关键:

So in my attempt to get this relationship to work I've switched the keys around in the following way:

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

但是,同样的错误,还是没有运气 - 我真的在失去为什么这种关系不起作用。对我来说似乎很简单。

However, same error, still no luck - I'm really at a loss why this relationship won't work. To me it seems pretty straight forward.

更新2

一个浪费时间的TON弄错了我已经尝试了一个稍微不同的方法:

Ok - after a TON of wasted time messing with this I've tried a slightly different approach:

这是我的新课程....

Here are my new 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; }
     }

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

No more error messages with the code above! The only problem is that the relationship entity framework is creating is between the Customer.CustomerId and Lead.LeadId instead of the Customer.LeadId and Lead.LeadId - I feel like i'm SO CLOSE!

可能的解决方案

好吧 - 经过一些更多的研究,我遇到这个帖子:
EF代码第一 - 1对1可选关系

Ok - so after some more research I came across this post here: 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);
}

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

Everything works GREAT! But one BIG problem...

我不得不从客户表中删除LeadId属性....所以现在我不知道如何分配一个LeadId创建新客户时(如果适用),如果没有LeadId属性分配给?

I had to remove the LeadId property from the Customer Table.... so now I'm not sure how I can assign a LeadId when creating a new Customer (when appropriate) if there is no LeadId property to assign to?

推荐答案

通过流畅的API发布,它应该工作。

Posting this in fluent API, it should work.

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

这篇关于无法确定主体终端 - 实体框架代码第一关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆