entity framework - EntityFramework CodeFirst Mapping to have camel case in the database -
i've started new company happy me use codefirst - want database fields camel cased.
i'm writing lot of kind of stuff ...
[column("active")] public bool active { get; set; } [column("paymenttype")] public string paymenttype { get; set; } is there way can set camel case database, rather having decorate properties?
thanks
you can use code first custom conventions if using fluent api, alternatively use reflection of each type in context. loop on each poco , and each property set name changing char1 lowercase.
modelbuilder.entity<eftestpoco>().property(p=>p.uom1).hascolumnname("camelcase"); edit: reflection challenge includes dynamic lambda... until asked didnt realise loop required dynamic lambda.... opened mouth wide :-)
... cut , paste pieces of code use. ... use easier /system.linq.dynamic approach
you can use expression complication library system.linq.expressions or can use easier use dynamic lambda library build dymanic linq statements.
so here sample code
// inside context on model creating //.... // repeat each poco. // or reflect on thr context if feeling lazy , intellectual var entity = new entitytypeconfiguration<poco>; // properties of 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) 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. // , teh rest may use... } } public static type underlyingtype(this propertyinfo propertyinfo) { return nullable.getunderlyingtype(propertyinfo.propertytype) ?? propertyinfo.propertytype; }
Comments
Post a Comment