Fabricio Rodriguez
Fabricio Rodriguez

Reputation: 4239

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?

I.e. 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 (i.e. migrations should generate this table):

person
    person
    firstname
    surname

or even something like this would be nice:

person
    person_id
    first_name
    surname

P.S. I am working with a MySQL database... Thanks

Upvotes: 4

Views: 2455

Answers (1)

yyc
yyc

Reputation: 298

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.

Upvotes: 1

Related Questions