Arm*_*ibi 7 c# entity-framework-core ef-core-5.0
如何更改 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; }
}
Run Code Online (Sandbox Code Playgroud)
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; }
}
Run Code Online (Sandbox Code Playgroud)
以及这两个名为 FoodMenu 的实体的连接表,我想将其更改为其他内容..
Iva*_*oev 20
您可以使用其中一种UsingEntity方法重载,例如UsingEntity(Action<EntityTypeBuilder>)。
既然是关系流畅的配置API,首先需要HasMany+WithMany对,例如
modelBuilder.Entity<Food>()
.HasMany(left => left.Menus)
.WithMany(right => right.Foods)
.UsingEntity(join => join.ToTable("TheDesiredName"));
Run Code Online (Sandbox Code Playgroud)
接受的答案缺少我必须努力解决的重要部分。
首先,您需要安装该软件包
Microsoft.EntityFrameworkCore.Relational
Run Code Online (Sandbox Code Playgroud)
OnModelCreating然后您可以在重写的方法中添加以下内容
modelBuilder.Entity<Food>()
.HasMany(left => left.Menus)
.WithMany(right => right.Foods)
.UsingEntity(join => join.ToTable("NameYouWish"));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2035 次 |
| 最近记录: |