Entity Framework 6 & TPH inheritance: Map properties with the same name to same column by default

后端 未结 1 479
Happy的楠姐
Happy的楠姐 2021-02-15 10:56

As of EF6 it is possible to do something like this when configuring Entity mappings using Table Per Hierarchy inheritance:

public class MyContext : DbContext 
{
         


        
1条回答
  •  南笙
    南笙 (楼主)
    2021-02-15 11:37

    Used Custom Code First Conventions, which are available from EF6 onwards, to sort this out:

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            //your code before
            modelBuilder.Properties().Configure(prop => prop.HasColumnName(prop.ClrPropertyInfo.Name));
            //your code after
        }
    

    This maps properties with the same name in different derived types to the same table column without explicit calls like those mentioned in the question.

    0 讨论(0)
提交回复
热议问题