c# - Entity Framework 表名称约定

标签 c# entity-framework

我正在使用 EF 6,有两个简单的 POCO 类,如下所示:

public class Person
 {
    public int PersonId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
 }

public class Company
{
    public int CompanyId { get; set; }
    public string Name { get; set; }
}

和我的背景

public class Context : DbContext
{
    public Context() : base("name=codefirst")
    {

    }
    public DbSet<Person> People { get; set; }
    public DbSet<Company> Corporation { get; set; }
}

和 EF 生成的表: dbo.Companiesdbo.People

我的问题是为什么一个表名是 People 而另一个表名是 Companies (我知道为什么是复数)。 我的意思是,一个表使用属性名,另一个表使用类名?

提前致谢!

enter image description here

最佳答案

两者都使用它们的类名进行映射。
可能让您感到困惑的是 Person 类的映射。它是 EF 复数规则之一,因为 people 是 person 的复数形式(意思是不止一个人),EF 将其映射为 People。 另一方面,它只是将公司类映射为公​​司。

您可以通过在 OnModelCreating() 中添加此代码来关闭 EF 复数约定。方法,如果你不喜欢它:

modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

关于c# - Entity Framework 表名称约定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41317615/

相关文章:

c# - 让 GetDecimal 发挥作用

c# - 无法使字节成为必填字段

c# - 删除从客户端发送的值列表

c# - 首先使用 EF 5 数据库进行延迟加载

c# - EF 和 TPT : the column name is specified more than once in the SET clause

C# 'System.InvalidOperationException' 无法在流程流上混契约(Contract)步和异步操作

c# - 如何在 .Net 中的唯一字符串集合中创建有保证的唯一哈希值?

c# - 异步读取文件的正确方法

c# - 将 Entity Framework 从 MVC 移动到另一个项目会导致重新迁移

c# - 影响 EF 代码中的外键列命名(CTP5)