相关疑难解决方法(0)

实体框架核心:与同一实体的多对多关系

我试图映射与同一实体的多对多关系.该User实体有一个IList<User>数据字段Contacts,用于存储用户的联系人/朋友信息:

public class User : DomainModel
{
    public virtual IList<User> Contacts { get; protected set; }
    //irrelevant code omitted
}
Run Code Online (Sandbox Code Playgroud)

当我尝试使用流畅的API来映射这么多对多的关系时,它给了我一些麻烦.显然,当我HasMany()在该user.Contacts属性上使用时,它没有WithMany()方法可以调用下一个.Visual Studio中的intellisense只显示WithOne(),但不显示WithMany().

modelBuilder.Entity<User>().HasMany(u => u.Contacts).WithMany() 
// gives compile time error: CS1061 'CollectionNavigationBuilder<User, User>' does not contain a definition for 'WithMany' and no extension method 'WithMany' accepting a first argument of type 
Run Code Online (Sandbox Code Playgroud)

那么为什么会这样呢?有没有什么我做错了映射这种多对多的关系?

c# linq entity-framework-core .net-core asp.net-core

19
推荐指数
1
解决办法
1万
查看次数

EF7(Core)中同一个表的多个关系

我有这样的模特

public class Question
{
    public string Id { get; set; } = Guid.NewGuid().ToString();

    public Answer Answer { get; set; }
    public List<Variant> Variants { get; set; }

    public string CorrectVariantId { get; set; }
    public Variant CorrectVariant { get; set; }
}

public class Variant
{
    public string Id { get; set; } = Guid.NewGuid().ToString();

    public string QuestionId { get; set; }
    public Question Question { get; set; }
}

// mapping

modelBuilder.Entity<Question>()
    .HasOne(q => q.CorrectVariant)
    .WithOne(v => v.Question) …
Run Code Online (Sandbox Code Playgroud)

.net c# orm entity-framework

8
推荐指数
3
解决办法
8301
查看次数