如何在 Entity Framework Core 6 中使用 Map 方法。升級到 Entity Framework Core 6 后,Map() 方法不再起作用。有沒有類似的東西可以用來將列映射到表?
下面是我的代碼示例。
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
foreach (var relationship in modelBuilder.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys()))
{
relationship.DeleteBehavior = DeleteBehavior.Restrict;
}
modelBuilder.Entity<RoleGroup>()
.HasMany(c => c.Roles).WithMany(i => i.RoleGroups).Map(t => t.MapLeftKey("RoleGroupId")
.MapRightKey("RoleId")
.ToTable("RoleGroupRole"));
}
uj5u.com熱心網友回復:
EF Core 的大多數示例都定義了 RoleGroupRole 物體,但它們確實支持將Dictionary<string, object>影子物體占位符用于基本連接表:
modelBuilder.Entity<RoleGroup>
.HasMany(u => u.Roles)
.WithMany(g => g.RoleGroups)
.UsingEntity<Dictionary<string, object>>(
right => right
.HasOne<Role>()
.WithMany(),
left => left
.HasOne<RoleGroup>()
.WithMany(),
join => join
.ToTable("RoleGroupRoles"));
這種配置的問題是兩邊的運算式先“右”然后“左”,很容易讓它們倒退。:)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/507003.html
