简体   繁体   中英

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?

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

Yes, it is possible, using Data Annotation [Table("table_name")] and [Column("column_name")].

A better way for column name is to write custom conventions in your OnModelCreating() method. For example, something like

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

And for your table 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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