如何在与其他类型具有其他关系的类型中定义 EF 多对多关系
How to define EF Many To Many Relationship in type that has other relationships to the same other type
这是一个很简单的问题,但解释很长,所以如果很长,请多多包涵。 (我真的会尽量减少它)
好的,这是我的 "extracted sample" 产生了同样的问题...
public class TestContext : DbContext
{
public DbSet<TypeA> TypeAs { get; set; }
public DbSet<TypeB> TypeBees { get; set; }
}
public class TypeA
{
[Key]
public int Id { get; set; }
public int SpecialBId { get; set; }
public virtual ICollection<TypeB> Bees { get; set; }
public virtual TypeB SpecialB { get; set; }
}
public class TypeB
{
[Key]
public int Id { get; set; }
public virtual ICollection<TypeA> Aaas { get; set; }
}
...所以我启用 EF 迁移,运行 添加迁移,运行 更新数据库,然后我得到这个...
我希望看到的是多对多的连接 table,但由于某种原因,第二个 "SpecialB" 引用导致它打破多对多引用/以某种方式重新定义它.
所以我想嗯,好吧……也许它只是需要帮助,所以我在我的上下文中添加了一个 onmodel 创建……
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<TypeA>().HasMany<TypeB>(i => i.Bees);
modelBuilder.Entity<TypeB>().HasMany<TypeA>(i => i.Aaas);
}
此时生成迁移不会产生任何结果。
So, how do I get these relationships to work
correctly and produce the correct db structure?
以防万一...
这是 Ef 在 运行 添加迁移以使用定义的类型为上面的数据库上下文创建初始迁移时生成的内容:
public partial class Initial : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.TypeAs",
c => new
{
Id = c.Int(nullable: false, identity: true),
SpecialBId = c.Int(nullable: false),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.TypeBs", t => t.SpecialBId, cascadeDelete: true)
.Index(t => t.SpecialBId);
CreateTable(
"dbo.TypeBs",
c => new
{
Id = c.Int(nullable: false, identity: true),
TypeA_Id = c.Int(),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.TypeAs", t => t.TypeA_Id)
.Index(t => t.TypeA_Id);
}
public override void Down()
{
DropForeignKey("dbo.TypeAs", "SpecialBId", "dbo.TypeBs");
DropForeignKey("dbo.TypeBs", "TypeA_Id", "dbo.TypeAs");
DropIndex("dbo.TypeBs", new[] { "TypeA_Id" });
DropIndex("dbo.TypeAs", new[] { "SpecialBId" });
DropTable("dbo.TypeBs");
DropTable("dbo.TypeAs");
}
}
您需要添加 WithMany():
modelBuilder.Entity<TypeA>().HasMany<TypeB>(i => i.Bees).WithMany();
modelBuilder.Entity<TypeB>().HasMany<TypeA>(i => i.Aaas).WithMany();
这是一个很简单的问题,但解释很长,所以如果很长,请多多包涵。 (我真的会尽量减少它)
好的,这是我的 "extracted sample" 产生了同样的问题...
public class TestContext : DbContext
{
public DbSet<TypeA> TypeAs { get; set; }
public DbSet<TypeB> TypeBees { get; set; }
}
public class TypeA
{
[Key]
public int Id { get; set; }
public int SpecialBId { get; set; }
public virtual ICollection<TypeB> Bees { get; set; }
public virtual TypeB SpecialB { get; set; }
}
public class TypeB
{
[Key]
public int Id { get; set; }
public virtual ICollection<TypeA> Aaas { get; set; }
}
...所以我启用 EF 迁移,运行 添加迁移,运行 更新数据库,然后我得到这个...
我希望看到的是多对多的连接 table,但由于某种原因,第二个 "SpecialB" 引用导致它打破多对多引用/以某种方式重新定义它.
所以我想嗯,好吧……也许它只是需要帮助,所以我在我的上下文中添加了一个 onmodel 创建……
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<TypeA>().HasMany<TypeB>(i => i.Bees);
modelBuilder.Entity<TypeB>().HasMany<TypeA>(i => i.Aaas);
}
此时生成迁移不会产生任何结果。
So, how do I get these relationships to work correctly and produce the correct db structure?
以防万一...
这是 Ef 在 运行 添加迁移以使用定义的类型为上面的数据库上下文创建初始迁移时生成的内容:
public partial class Initial : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.TypeAs",
c => new
{
Id = c.Int(nullable: false, identity: true),
SpecialBId = c.Int(nullable: false),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.TypeBs", t => t.SpecialBId, cascadeDelete: true)
.Index(t => t.SpecialBId);
CreateTable(
"dbo.TypeBs",
c => new
{
Id = c.Int(nullable: false, identity: true),
TypeA_Id = c.Int(),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.TypeAs", t => t.TypeA_Id)
.Index(t => t.TypeA_Id);
}
public override void Down()
{
DropForeignKey("dbo.TypeAs", "SpecialBId", "dbo.TypeBs");
DropForeignKey("dbo.TypeBs", "TypeA_Id", "dbo.TypeAs");
DropIndex("dbo.TypeBs", new[] { "TypeA_Id" });
DropIndex("dbo.TypeAs", new[] { "SpecialBId" });
DropTable("dbo.TypeBs");
DropTable("dbo.TypeAs");
}
}
您需要添加 WithMany():
modelBuilder.Entity<TypeA>().HasMany<TypeB>(i => i.Bees).WithMany();
modelBuilder.Entity<TypeB>().HasMany<TypeA>(i => i.Aaas).WithMany();