c# - 代码优先更改桥实体表的名称

标签 c# asp.net-mvc entity-framework ef-code-first

有没有办法控制桥实体创建的表的名称?现在如果我运行这个:

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/

相关文章:

c# - 在 LINQ 查询中将枚举转换为字符串时出错

c# - 反序列化 xml,其中字符串可能包含 xml/html

c# - 如何从 infoPath 表单到 Sharepoint 库生成具有唯一 ID 的名称? C#

html - @Html.EditorFor 中的属性 css

asp.net-mvc - ASP .NET MVC 客户端验证是否有一些 "is valid"标志?

c# - 让 IQueryable<int> 集合在 foreach 循环中工作的奇怪问题

c# - MiniProfiler、EntityFramework 代码优先和后台任务 nullreference

c# - 查找最接近的 "nice"标尺图 block 宽度

c# - 关闭 MSSQL 服务器中的数据库连接

jquery - 为什么从 Jquery 1.5.1 更改为 Jquery 1.8.1 会破坏我在 MVC3 中的所有 CSS?