0

嗨,我有一个模型类

    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’的定义,并且找不到接受‘CollectionNavigationBuilder’类型的第一个参数的可访问扩展方法‘WithMany’(您是否缺少使用指令还是程序集参考?)存储库 C:\Users\Luka\Desktop\Individuals\Repositories\Context\IndividualsDbContext.cs 23 Active "

有什么建议么 ?如何正确地做到这一点?

编辑后:我试过了

    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 的基础时请帮助。

4

1 回答 1

0

您将如何将这些信息存储在单个数据库表中?

但即使你可以 - EF 核心也不直接支持多对多关系。您需要定义连接表,以便拥有两个一对多的关系。这就是数据库建模的工作原理。

EF 6 确实隐式创建了一个连接表,但幸运的是 EF 核心没有

于 2019-12-14T11:52:34.017 回答