有没有办法控制桥实体创建的表的名称?现在如果我运行这个:
public class Foo
{
public Int32 FooId { get; set; }
public virtual ICollection<Bar> Bars { get; set; }
}
public class Bar
{
public Int32 BarId { get; set; }
public virtual ICollection<Foo> Foos { get; set; }
}
生成的桥接表名为 BarFoo,有什么方法可以将其改为 FooBar 吗?
最佳答案
您可以通过如下映射来自定义表名称。
public class MyContext : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Foo>()
.HasMany(e => e.Bars)
.WithMany(s => s.Foos)
.Map(l =>
{
l.ToTable("FooBar");
l.MapLeftKey("FooId");
l.MapRightKey("BarId");
}
);
}
}
关于c# - 代码优先更改桥实体表的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8995854/