EntityFramework CodeFirst映射在数据库中具有骆驼案例 [英] EntityFramework CodeFirst Mapping to have camel case in the database

查看:94
本文介绍了EntityFramework CodeFirst映射在数据库中具有骆驼案例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开了一家新公司,对我来说很开心使用CodeFirst,但是希望数据库中的骆驼套装。

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; }

有没有什么办法可以将它全部设置为Camel Case的数据库,而不是必须装饰我的所有属性?

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

谢谢

推荐答案

a href =http://entityframework.codeplex.com/wikipage?title=Custom%20Conventions =nofollow>代码第一个自定义约定
如果使用Fluent Api,您可以使用反射在您的上下文中的每个类型。
在每个POCO上循环,并为每个PROPERTY设置名称将char1更改为小写。

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");

编辑:反思的挑战包括动态lambda ...
直到你问我没有意识到循环需要动态lambda ....
打开我的嘴太宽: - )

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

...这是一个剪切和粘贴我使用的代码片段。
...我使用EASIER /System.Linq.Dynamic方法

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

您可以使用Expression Complication库 System.Linq.Expressions
或者您可以使用更容易使用的动态Lambda库
构建dymanic Linq语句。

SO这里是示例代码

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;
    }

这篇关于EntityFramework CodeFirst映射在数据库中具有骆驼案例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆