EntityFramework CodeFirst Mapping to have camel case in the database

旧街凉风 提交于 2019-12-31 03:46:05

问题


I've just started a new company who are happy for me to use CodeFirst - but want the database fields camel cased.

I'm currently writing a lot of this kind of stuff ...

    [Column("active")]
    public bool Active { get; set; }

    [Column("paymentType")]
    public string PaymentType { get; set; }

Is there any way I can just set it all to Camel Case the database, rather than having to decorate all my properties?

Thanks


回答1:


You can use code first custom conventions If using Fluent Api, you could alternatively use reflection of each Type in your context. Loop on each POCO and and for each PROPERTY set the name changing char1 to lowercase.

modelBuilder.Entity<EFTestPoco>().Property(p=>p.UoM1).HasColumnName("camelCase");

EDIT: The Reflection challenge includes dynamic lambda... Until you asked I didnt realise that the loop required dynamic lambda.... Opened my mouth too wide :-)

... This is a cut and paste from pieces of code i use. ... I use the EASIER /System.Linq.Dynamic approach

You can use the Expression Complication library System.Linq.Expressions or You can use the easier to use Dynamic Lambda library to build dymanic Linq statements.
SO here is the sample code

  // inside your context on model creating
  //....   
 // repeat for each poco.  // or reflect on thr context if feeling lazy and intellectual
 var entity = new EntityTypeConfiguration<Poco>;
// Get the properties of a poco
    foreach (var propInfo in typeof(T).GetProperties()) {
            SetCamelCase<T>(propInfo,entity);
        }
 modelBuilder.Configurations.Add(entity);
 ....
 } // end of ON model creating



private static void SetCamelCase<TModelPoco>(PropertyInfo propertyInfo, 
                             EntityTypeConfiguration<TModelPoco> entity) where TModelPoco : BaseObject {

        var camel = propertyInfo.Name.Substring(0, 1).ToLower() + propertyInfo.Name.Substring(1);

        switch (propertyInfo.UnderLyingType().Name) {
            case SystemDataTypeConstants.String :
            var propLambdaString = System.Linq.Dynamic.DynamicExpression.ParseLambda<TModelPoco, string >(propertyInfo.Name);
            entity.Property(propLambdaString).HasColumnName(camel);
            break;
            case SystemDataTypeConstants.Int32:
            var propLambdaInt =System.Linq.Dynamic.DynamicExpression.ParseLambda<TModelPoco, int >(propertyInfo.Name);
            entity.Property(propLambdaInt).HasColumnName(camel);
                break;
           //  SystemDataTypeConstants. // and teh rest you may use...
        }


    }
    public static Type UnderLyingType(this PropertyInfo propertyInfo) {
        return Nullable.GetUnderlyingType(propertyInfo.PropertyType) ?? propertyInfo.PropertyType;
    }


来源:https://stackoverflow.com/questions/16563267/entityframework-codefirst-mapping-to-have-camel-case-in-the-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!