[英]EF Core 5 Many-to-Many relation - Change the Navigation key
我在 EF Core 5 中有两个具有多对多关系的实体,我正在使用流利的 API 来设置关系。
public class Framework
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long Id { get; set; }
[Required]
public string Name { get; set; }
public IList<Path> Paths { get; set; } = new List<Path>();
}
public class Path
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long Id { get; set; }
[Required]
public string Name { get; set; }
public IList<Framework> Frameworks { get; set; } = new List<Framework>();
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<Framework>()
.HasMany(x => x.Paths)
.WithMany(x => x.Frameworks)
.UsingEntity<Dictionary<string, object>>(
"FrameworkPaths",
x => x.HasOne<Path>().WithMany(),
x => x.HasOne<Framework>().WithMany(),
);
它工作正常,并且正在将数据添加到多对多表中(其中包含这些列FrameworksId , PathsId )
但是现在我想将Path实体中的属性名称从Frameworks更改为LinkedFrameworks作为
public IList<Framework> LinkedFrameworks { get; set; } = new List<Framework>();
如何在实体的 CRUD 操作中使用这个新的属性名称LinkedFrameworks而不是Framework ?
它是通过在 usingEntity 中添加 the.HasForeignKey() 来实现的
builder.Entity<Framework>()
.HasMany(x => x.Paths)
.WithMany(x => x.LinkedFrameworks) //Change the name here
.UsingEntity<Dictionary<string, object>>(
"FrameworkPaths",
x => x.HasOne<Path>().WithMany(),
x => x.HasOne<Framework>().WithMany().HasForeignKey("FrameworksId")); //tell the EF about the Foreign Key
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.