Entity Framework Core:与同一实体的多对多关系 [英] Entity Framework Core: many-to-many relationship with same entity

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

问题描述

我正在尝试映射与同一实体的多对多关系.User 实体有一个用于 ContactsIList 数据字段,用于存储用户的联系人/朋友信息:

I am trying to map many-to-many relationship with the same entity. The User entity has an IList<User> data field for Contacts, which stores users' contacts/friends information:

public class User : DomainModel
{
    public virtual IList<User> Contacts { get; protected set; }
    //irrelevant code omitted
}

当我尝试使用 fluent API 来映射这种多对多关系时,它给我带来了一些麻烦.显然,当我在 user.Contacts 属性上使用 HasMany() 时,它没有 WithMany() 方法可以调用 next .Visual Studio 的智能感知仅显示 WithOne(),而不显示 WithMany().

When I try to use fluent API to map this many to many relationship, it gives me some trouble. Apparently, when I use HasMany() on the user.Contacts property, it has no WithMany() method to call next. The intellisense from Visual Studio only shows WithOne(), but not WithMany().

modelBuilder.Entity<User>().HasMany(u => u.Contacts).WithMany() 
// gives compile time error: CS1061 'CollectionNavigationBuilder<User, User>' does not contain a definition for 'WithMany' and no extension method 'WithMany' accepting a first argument of type 

那为什么会这样呢?映射这种多对多关系有什么我做错了吗?

So why does this happen? Is there anything I did wrong to map this many-to-many relationship?

推荐答案

那为什么会这样呢?我做错了什么来映射这个多对多关系?

So why does this happen? Is there anything I did wrong to map this many-to-many relationship?

不,你没有做错任何事.只是不支持.当前状态此处.

No, you didn't do anything wrong. It's just not supported. Current status here.

没有实体类来表示的多对多关系尚不支持连接表.但是,您可以代表一个通过为连接包含实体类来建立多对多关系表并映射两个单独的一对多关系.

Many-to-many relationships without an entity class to represent the join table are not yet supported. However, you can represent a many-to-many relationship by including an entity class for the join table and mapping two separate one-to-many relationships.

使用 EF-Core,您应该为映射表创建实体.例如UserContacts.如前所述,docs 中的完整示例在评论中.我还没有真正测试过下面的代码,但它应该是这样的:

With EF-Core you should create the entity for the mapping table. Such as UserContacts. A complete example in the docs, as mentioned in the comments. I haven't actually tested the code below, but it should look something like this:

public class UserContacts
{
    public int UserId { get; set; } 
    public virtual User User { get; set; }

    public int ContactId { get; set; } // In lack of better name.
    public virtual User Contact { get; set; }
}

public class User : DomainModel
{
    public List<UserContacts> Contacts { get; set; }
}

还有你的 modelBuilder.

  modelBuilder.Entity<UserContacts>()
        .HasOne(pt => pt.Contact)
        .WithMany(p => p.Contacts)
        .HasForeignKey(pt => pt.ContactId);

    modelBuilder.Entity<UserContacts>()
        .HasOne(pt => pt.User)
        .WithMany(t => t.Contacts)
        .HasForeignKey(pt => pt.UserId);

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

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