嗨,我有一个模型类
public class Individual : EntityBase
{
public Individual()
{
RelatedIndividuals = new HashSet<RelatedIndividual>();
}
public string FirstName { get; set; }
public string PersonalNumber { get; set; }
public ICollection<RelatedIndividual> RelatedIndividuals { get; set; }
}
此个人将具有一些具有相关类型的相关个人,我希望在同一表中具有多对多连接,因此我创建了另一个类
public class RelatedIndividual
{
[Column("RelatedIndividualType")]
public RelatedIndividualType RelatedIndividualType { get; set; }
[Key, Column(Order = 0)]
public long IndividualId { get; set; }
[Key, Column(Order = 1)]
public long RelatedIndividualId { get; set; }
}
我试图在我的contextDB中做
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Individual>()
.HasMany(p => p.RelatedIndividuals)
.WithMany()
.Map(m =>
{
m.MapLeftKey("IndividualId");
m.MapRightKey("RelatedIndividualId");
m.ToTable("RelatedIndividual");
});
}
但是我在WithMany上出错
“严重性代码描述项目文件行抑制状态 错误CS1061'CollectionNavigationBuilder'不包含'WithMany'的定义,并且找不到可以接受的扩展方法'WithMany'接受类型为'CollectionNavigationBuilder'的第一个参数(您是否缺少using指令或程序集引用?)存储库C: \ Users \ Luka \ Desktop \ Individuals \ Repositories \ Context \ IndividualsDbContext.cs 23活动 “
有什么建议吗?如何正确执行?
之后编辑: 我尝试过
public class Individual : EntityBase
{
public Individual()
{
RelatedIndividuals = new HashSet<RelatedIndividual>();
RelatedIndividualsOf = new HashSet<RelatedIndividual>();
}
[MaxLength(50),MinLength(2), Column(TypeName = "nvarchar(50)")]
public string FirstName { get; set; }
[MaxLength(50), MinLength(2), Column(TypeName = "nvarchar(50)")]
public string LastName { get; set; }
public ICollection<RelatedIndividual> RelatedIndividuals { get; set; }
public ICollection<RelatedIndividual> RelatedIndividualsOf { get; set; }
}
和我的第二堂课
public class RelatedIndividual
{
[Column("RelatedIndividualType")]
public RelatedIndividualType RelatedIndividualType { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public long IndividualId { get; set; }
[ForeignKey("IndividualId")]
public Individual Individual { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public long RelateIndividualId { get; set; }
[ForeignKey("RelateIndividualId")]
public Individual RelateIndividual { get; set; }
}
我的上下文如下
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<RelatedIndividual>()
.HasKey(e => new { e.IndividualId, e.RelateIndividualId });
modelBuilder.Entity<RelatedIndividual>()
.HasOne(pt => pt.Individual)
.WithMany(p => p.RelatedIndividuals)
.HasForeignKey(pt => pt.IndividualId);
modelBuilder.Entity<RelatedIndividual>()
.HasOne(pt => pt.RelateIndividual)
.WithMany(t => t.RelatedIndividualsOf)
.HasForeignKey(pt => pt.RelateIndividualId)
.OnDelete(DeleteBehavior.Restrict);
}
当我创建自然人时,它可以正常工作,当我创建自然人时,在自然人中,我添加RelatedIndividual,Ids不匹配,并且它同时添加了RelatedIndividuals和relatedIndividualsOf,例如,如果我新创建的个人是Id 2和当基数保存时,我将他与ID 1相关 请2、2而不是1、2帮助。
答案 0 :(得分:0)
您如何将这些信息存储在单个数据库表中?
但是,即使可以-EF核心也不直接支持多对多关系。您需要定义联接表,以便您具有两个一对多关系。这就是数据库建模的工作方式。
EF 6确实隐式创建了一个联接表,但是幸运的是EF core没有创建