[英]Error in getting navigation property in a many to many relationship in entity framework core
[英]Entity Framework Core Many to Many change navigation property names
我有一個名為“LogBookSystemUsers”的表,我想在 EF Core 5 中設置多對多的功能。我幾乎可以讓它工作,但問題是我的 ID 列被命名為SystemUserId
和LogBookId
但是當 EF 進行連接時,它會嘗試使用SystemUserID
和LogBookID
。 這是我當前的配置代碼:
modelBuilder.Entity<SystemUser>()
.HasMany(x => x.LogBooks)
.WithMany(x => x.SystemUsers)
.UsingEntity(x =>
{
x.ToTable("LogBookSystemUsers", "LogBooks");
});
我試過這個:
modelBuilder.Entity<SystemUser>()
.HasMany(x => x.LogBooks)
.WithMany(x => x.SystemUsers)
.UsingEntity<Dictionary<string, object>>("LogBookSystemUsers",
x => x.HasOne<LogBook>().WithMany().HasForeignKey("LogBookId"),
x => x.HasOne<SystemUser>().WithMany().HasForeignKey("SystemUserId"),
x => x.ToTable("LogBookSystemUsers", "LogBooks"));
但這只是添加了兩個新列,而不是設置當前列的名稱。
這首先是所有數據庫。 我不想為多對多表使用 class,因為我在我的項目中全部執行此操作,並且我不想要一堆無用的類。 有任何想法嗎?
有趣的錯誤,請考慮將其發布到 EF Core GitHub 問題跟蹤器。
根據想法,您嘗試過的應該這樣做
modelBuilder.Entity<SystemUser>()
.HasMany(x => x.LogBooks)
.WithMany(x => x.SystemUsers)
.UsingEntity<Dictionary<string, object>>("LogBookSystemUsers",
x => x.HasOne<LogBook>().WithMany().HasForeignKey("LogBookId"),
x => x.HasOne<SystemUser>().WithMany().HasForeignKey("SystemUserId"),
x => x.ToTable("LogBookSystemUsers", "LogBooks"));
當相關實體 PK 屬性稱為ID
時,它適用於除{RelatedEntity}Id
之外的任何其他 FK 屬性名稱。
作為解決方法,直到它得到修復,在配置關系之前明確定義所需的連接實體屬性:
// add this
modelBuilder.SharedTypeEntity<Dictionary<string, object>>("LogBookSystemUsers", builder =>
{
builder.Property<int>("LogBookId");
builder.Property<int>("SystemUserId");
});
// same as the original
modelBuilder.Entity<SystemUser>()
.HasMany(x => x.LogBooks)
.WithMany(x => x.SystemUsers)
.UsingEntity<Dictionary<string, object>>("LogBookSystemUsers",
x => x.HasOne<LogBook>().WithMany().HasForeignKey("LogBookId"),
x => x.HasOne<SystemUser>().WithMany().HasForeignKey("SystemUserId"),
x => x.ToTable("LogBookSystemUsers", "LogBooks"));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.