4

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

Fabricio Rodriguez
  • 3,769
  • 11
  • 48
  • 101
  • 1
    I have the same question. I'm using PostgreSQL which is case-sensitive and I also want lower-case table and column names. Did you find a solution? – Paul Meems Feb 21 '17 at 09:28

1 Answers1

1

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.

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
yyc
  • 288
  • 1
  • 4
  • 16