实体框架 6 2 * 多对多自我
Entity Framework6 2 * many-to-many to self
使用代码优先 Entity Framework 6.
我有这个实体:
public class LineSection
{
public int Id { get; set; }
public List<LineSection> Next { get; set; }
public List<LineSection> Previous { get; set; }
}
我添加了一个迁移,只是为了看看数据库会怎样:
CreateTable(
"dbo.LineSectionLineSections",
c => new
{
LineSection_Id = c.Int(nullable: false),
LineSection_Id1 = c.Int(nullable: false),
})
.PrimaryKey(t => new { t.LineSection_Id, t.LineSection_Id1 })
.ForeignKey("dbo.LineSections", t => t.LineSection_Id)
.ForeignKey("dbo.LineSections", t => t.LineSection_Id1)
.Index(t => t.LineSection_Id)
.Index(t => t.LineSection_Id1);
我不喜欢默认命名。我可以更改表名 (LineSectionLineSections) 和两个外键(LineSection_Id 和 LineSection_Id1)吗?使用模型构建器、数据属性或其他方式?
使用隐式连接 table 的多对多关系配置(无论 t 是否为 self 都无关紧要)使用 Map
流畅 API 执行。可以使用ToTable
指定table名称,MapLeftKey
/MapRightKey
指定对应的列名(左边是正在配置的实体,右边是另一端的实体)关系)。
所以在你的情况下它会是这样的:
modelBuilder.Entity<LineSection>()
.HasMany(e => e.Next)
.WithMany(e => e.Previous)
.Map(m => m.MapLeftKey("PrevId")
.MapRightKey("NextId")
.ToTable("LineSectionLinks")
);
使用代码优先 Entity Framework 6.
我有这个实体:
public class LineSection
{
public int Id { get; set; }
public List<LineSection> Next { get; set; }
public List<LineSection> Previous { get; set; }
}
我添加了一个迁移,只是为了看看数据库会怎样:
CreateTable(
"dbo.LineSectionLineSections",
c => new
{
LineSection_Id = c.Int(nullable: false),
LineSection_Id1 = c.Int(nullable: false),
})
.PrimaryKey(t => new { t.LineSection_Id, t.LineSection_Id1 })
.ForeignKey("dbo.LineSections", t => t.LineSection_Id)
.ForeignKey("dbo.LineSections", t => t.LineSection_Id1)
.Index(t => t.LineSection_Id)
.Index(t => t.LineSection_Id1);
我不喜欢默认命名。我可以更改表名 (LineSectionLineSections) 和两个外键(LineSection_Id 和 LineSection_Id1)吗?使用模型构建器、数据属性或其他方式?
使用隐式连接 table 的多对多关系配置(无论 t 是否为 self 都无关紧要)使用 Map
流畅 API 执行。可以使用ToTable
指定table名称,MapLeftKey
/MapRightKey
指定对应的列名(左边是正在配置的实体,右边是另一端的实体)关系)。
所以在你的情况下它会是这样的:
modelBuilder.Entity<LineSection>()
.HasMany(e => e.Next)
.WithMany(e => e.Previous)
.Map(m => m.MapLeftKey("PrevId")
.MapRightKey("NextId")
.ToTable("LineSectionLinks")
);