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

Error with many-to-many relationship in Entity Framework Core

我正在尝试使用 EF Core 实现以下方案。

这是我的 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>();
}

这是我的配置

使用Microsoft.EntityFrameworkCore; 使用 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"));

    }
}

这是错误:

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

EF Core(目前)不支持自动多对多关系。您必须在两端之间提供 link table:

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