简体   繁体   English

Entity Framework Core 中的多对多关系错误

[英]Error with many-to-many relationship in Entity Framework Core

I'm trying to achieve the Following scheme with EF Core.我正在尝试使用 EF Core 实现以下方案。

Here is my class这是我的 class

public class User
{
    this.Id = Guid.NewGuid().ToString();
    public ICollection<User> Followers { get; set; } = new List<User>();
    public ICollection<User> Following { get; set; } = new List<User>();
}

Here is my configuration这是我的配置

using Microsoft.EntityFrameworkCore;使用 Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Builders;使用 Microsoft.EntityFrameworkCore.Metadata.Builders;

public class UserConfiguration : IEntityTypeConfiguration<User>
{
    public void Configure(EntityTypeBuilder<User> builder)
    {
        builder
            .HasMany(m => m.Followers)
            .WithMany(m => m.Following)
            .Map(x => x.MapLeftKey("UserId")
            .MapRightKey("FollowerId")
            .ToTable("UserFollowers"));

    }
}

Here is Error:这是错误:

Severity Code Description Project File Line Suppression State Error CS1061 'CollectionNavigationBuilder<User, User>' does not contain a definition for 'WithMany' and no accessible extension method 'WithMany' accepting a first argument of type 'CollectionNavigationBuilder<User, User>' could be found (are you missing a using directive or an assembly reference?) Gapped.Entities E:\Projects\Generic\GappedProfileAPI\GappedBaseAPI-skeleton\Gapped.Entities\Models\Configurations\Users\UserConfiguration.cs 50 Active严重性代码说明项目文件行抑制 State 错误 CS1061“CollectionNavigationBuilder<User, User>”不包含“WithMany”的定义,并且没有可访问的扩展方法“WithMany”接受类型为“CollectionNavigationBuilder<User, User>”的第一个参数找到(是否缺少 using 指令或程序集引用?)Gapped.Entities E:\Projects\Generic\GappedProfileAPI\GappedBaseAPI-skeleton\Gapped.Entities\Models\Configurations\Users\UserConfiguration.cs 50 活动

EF Core doesn't (yet) support automatic many to many relationships. EF Core(目前)不支持自动多对多关系。 You have to provide the link table between the 2 ends:您必须提供两端之间的链接表:

public class User
{
    this.Id = Guid.NewGuid().ToString();
    public ICollection<Follower> Followers { get; set; } = new List<Follower>();
    public ICollection<Follower> Following { get; set; } = new List<Follower>();
}

public class Follower
{
    public User User {get;set;}
    public User Follower {get;set;}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM