如何解决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;}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。