簡體   English   中英

Entity Framework Core 多對多更改導航屬性名稱

[英]Entity Framework Core Many to Many change navigation property names

我有一個名為“LogBookSystemUsers”的表,我想在 EF Core 5 中設置多對多的功能。我幾乎可以讓它工作,但問題是我的 ID 列被命名為SystemUserIdLogBookId但是當 EF 進行連接時,它會嘗試使用SystemUserIDLogBookID 這是我當前的配置代碼:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM