[英]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"));
}
}
这是错误:
严重性代码说明项目文件行抑制 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(目前)不支持自动多对多关系。 您必须提供两端之间的链接表:
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.