简体   繁体   English

EF代码首先从Pascal Case Properties迁移到小写字母列

[英]EF code first migrations from Pascal Case Properties to lower case columns

In Entity Framework 6 Code First, is there a way (perhaps via Data Annotations or Fluent API) so that the database generated by Migrations has lower case column names, even though my model classes have Pascal Casing properties? 在Entity Framework 6 Code First中,有没有一种方法(也许通过数据注释或Fluent API),这样即使我的模型类具有Pascal Casing属性,Migrations生成的数据库也具有小写的列名?

Ie This class: 即本课程:

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

should map to this table (ie migrations should generate this table): 应该映射到该表(即迁移应生成此表):

person
    person
    firstname
    surname

or even something like this would be nice: 甚至像这样的东西都会很好:

person
    person_id
    first_name
    surname

PS I am working with a MySQL database... Thanks PS我正在使用MySQL数据库...谢谢

Yes, it is possible, using Data Annotation [Table("table_name")] and [Column("column_name")]. 是的,可以使用数据注释[Table(“ table_name”)]和[Column(“ column_name”)]。

A better way for column name is to write custom conventions in your OnModelCreating() method. 列名的一种更好的方法是在OnModelCreating()方法中编写自定义约定 For example, something like 例如,类似

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

And for your table id 和你的表ID

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

I am not sure about custom convention for table name, but my personal preference is to use Data Annotations for my tables. 我不确定表名的自定义约定,但是我个人的喜好是对表使用数据注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM