簡體   English   中英

更改生成的連接表的名稱(多對多)-EF Core 5

[英]Change name of generated Join table ( Many to Many ) - EF Core 5

如何更改 EF Core 5 創建的連接表的名稱?

例如

 public class Food 
    {
        public int FoodId { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public string Ingredients { get; set; }
        public string PhotoPath { get; set; }
        public ICollection<Menu> Menus { get; set; }
    }

  public class Menu
    {
        public int MenuId { get; set; }
        [Column(TypeName = "date")]
        public DateTime MenuDate { get; set; }
        public bool IsPublished { get; set; }
        public ICollection<Food> Foods { get; set; }
    }

以及這 2 個名為 FoodMenu 的實體的連接表,我想將其更改為其他內容..

您可以使用UsingEntity<\/code><\/a>方法重載之一,例如UsingEntity(Action<EntityTypeBuilder>)<\/code><\/a> 。

由於它是一個關系流式配置 API,您首先需要HasMany<\/code> + WithMany<\/code>對,例如

modelBuilder.Entity<Food>()
    .HasMany(left => left.Menus)
    .WithMany(right => right.Foods)
    .UsingEntity(join => join.ToTable("TheDesiredName"));
   

接受的答案缺少我必須努力解決的重要部分。

首先,您需要安裝軟件包

Microsoft.EntityFrameworkCore.Relational

然后你可以在你的OnModelCreating重寫方法中添加以下內容

modelBuilder.Entity<Food>()
    .HasMany(left => left.Menus)
    .WithMany(right => right.Foods)
    .UsingEntity(join => join.ToTable("NameYouWish"));

是的,我的錯,你是對的@IvanStoev 它在我的本地代碼上......真的很抱歉,我的眼睛沒有看到它。

謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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