【问题标题】:EF code first migrations from Pascal Case Properties to lower case columnsEF 代码首先从 Pascal Case Properties 迁移到小写列
【发布时间】:2017-02-07 07:43:56
【问题描述】:

在 Entity Framework 6 Code First 中,有没有办法(可能通过 Data Annotations 或 Fluent API)让 Migrations 生成的数据库具有小写的列名,即使我的模型类具有 Pascal Casing 属性?

即这个类:

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

应该映射到这个表(即迁移应该生成这个表):

person
    person
    firstname
    surname

或者像这样的东西会很好:

person
    person_id
    first_name
    surname

附:我正在使用 MySQL 数据库...谢谢

【问题讨论】:

  • 我也有同样的问题。我正在使用区分大小写的 PostgreSQL,并且我还想要小写的表和列名。你找到解决办法了吗?

标签: .net ef-code-first entity-framework-6 entity-framework-migrations ef-code-first-mapping


【解决方案1】:

是的,可以使用数据注释 [Table("table_name")] 和 [Column("column_name")]。

列名的更好方法是在 OnModelCreating() 方法中使用 write custom conventions。例如,像

modelBuilder
    .Properties()
    .Configure(p => p.HasColumnName(p.ClrPropertyInfo.Name.ToLower()));

对于你的表 id

modelBuilder
    .Properties()
    .Configure(p => p.IsKey().HasColumnName(///the name you want///));

我不确定表名称的自定义约定,但我个人的偏好是为我的表使用数据注释。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-26
    • 1970-01-01
    • 1970-01-01
    • 2016-03-18
    • 2023-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多