使用同一实体的一对一关系+一对多关系 [英] One-to-one relation + on-to-many relation using same entity

查看:102
本文介绍了使用同一实体的一对一关系+一对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个具有多个联系人和一个主要联系人的客户实体,但是由于出现以下错误,我似乎无法添加迁移:

I'm trying to create a customer entity that has multiple contact persons, as well as one primary contact person, but I can't seem to add the migration, as I'm getting the following error:


无法确定类型为 Customer的导航属性 ContactPerson.Customer表示的关系。要么手动配置关系,要么使用 [NotMapped]属性或 OnModelCreating中的 EntityTypeBuilder.Ignore忽略此属性。

Unable to determine the relationship represented by navigation property 'ContactPerson.Customer' of type 'Customer'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

客户

public class Customer
{
    public Guid CustomerId { get; set; }
    public string Name { get; set; }
    public DateTime CreateDate { get; set; }
    public string City { get; set; }
    public string Address { get; set; }

    // Contact person data
    public virtual ContactPerson PrimaryContactPerson { get; set; }
    public virtual ICollection<ContactPerson> ContactPersons { get; set; }
}

ContactPerson

public class ContactPerson
{
    public Guid ContactPersonId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public virtual Customer Customer { get; set; }
}

我尝试自己添加外键,并使用以下命令注释外键属性我的实体,在 Customer 实体以及 ContactPerson 实体上,如下所示:

I tried adding the foreign keys myself, and annotating the foreign key property with my entity, on both the Customer entity as well as the ContactPerson entity, like this:

public class ContactPerson
{
    public Guid ContactPersonId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

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

但似乎没有什么不同,我仍然得到同样的错误。 EF无法确定这种关系?

But it doesn't seem to make any difference, I'm still getting the same error. How can it be that EF can't determine the relationship?

我想这与同时存在于一对多关系中的一对一关系有关,但我似乎无法把我的想法包起来解决这个问题。

I suppose it has something to do with the one-to-one relation simultaneously existing with the one-to-many relation, but I can't seem to wrap my head around this issue. Advice and suggestion are highly appreciated!

如果我注释掉 PrimaryContactPerson 属性,那么EF可以很好地添加迁移,因此,我很肯定这与两种不同的关系有关。

If I comment out the PrimaryContactPerson property, EF adds the migration just fine, so I'm positive that this has something to do with the two different relations.

推荐答案

我设法使用实体框架Fluent API,如下所示:

I managed to solve my issue using the Entity Framework Fluent API, like this:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<ContactPerson>(e =>
        e.HasOne(r => r.Customer).WithMany(c => c.ContactPersons)
    );
}

这篇关于使用同一实体的一对一关系+一对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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