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

.net ef-code-first ef-code-first-mapping ef-migrations entity-framework-6

Question

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

1
2
2/7/2017 7:43:56 AM

Popular Answer

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.

0
10/10/2018 1:39:52 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow