Entity Framework Core

问题描述

我正在尝试通过EF Core实现以下方案。

这是我的课程

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"));

    }
}

这里是错误:

严重性代码描述项目文件行抑制状态 错误CS1061“ CollectionNavigationBuilder ”不包含“ WithMany”的定义,并且找不到可访问的扩展方法“ WithMany”接受类型为“ CollectionNavigationBuilder ”的第一个参数(您是否缺少使用指令还是程序集引用?)Gapped.Entities E:\ Projects \ Generic \ GappedProfileAPI \ GappedBaseAPI-skeleton \ Gapped.Entities \ Models \ Configurations \ Users \ UserConfiguration.cs 50有效

解决方法

EF Core目前还不支持自动多对多关系。您必须提供两端之间的链接表:

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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...