2013-05-15 1 views
1

저는 CodeFirst를 사용하여 기쁘게 생각하는 새로운 회사를 막 시작했습니다. 그러나 데이터베이스 필드는 낙타를 원합니다. 나는 현재 이런 종류의 물건을 많이 쓰고 있어요EntityFramework 데이터베이스에 낙타 사례가있는 CodeFirst 매핑

...

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

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

난 그냥 오히려 내 모든 속성을 장식하는 것보다, 낙타 케이스로 데이터베이스를 모두 설정할 수있는 방법이 있습니까 ?

감사

+0

낙타 경우 귀하의 POCO 회원입니까? –

답변

1

유창함 API를 사용하여, 당신은 다른 방법으로 당신의 맥락에서 각 유형의 반사를 사용할 수 있다면 당신은 code first custom conventions 를 사용할 수 있습니다. 각 POCO 및 루프마다 각 PROPERTY에 대해 char1을 소문자로 변경하는 이름을 설정하십시오.

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

편집 : 반성 도전은 ... 동적 람다를 포함 내가 루프가 .... 동적 람다 필요 이 :-)에게 너무 넓

을 내 입을 열어 실현 didnt는 요청까지 ... 이것은 내가 사용하는 코드 조각으로부터 잘라내어 붙여 넣기입니다. ... 난 당신이 식 합병증 라이브러리 System.Linq.Expressions 를 사용하거나 당신은 dymanic Linq에 문을 구축 할 Dynamic Lambda library 를 사용하여 쉽게 사용할 수있는 쉬운 /System.Linq.Dynamic 접근

를 사용합니다.
는 SO 여기 샘플 코드

// 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; 
    } 
+0

나는 두 번째 제안을 충분히 행복하게 하겠지만, 속성 선택기를 사용하여 엔터티 < "- 속성 만"속성 "을 해제 할 수는 없습니다. 실제로 속성을 반복하는 방법은 무엇입니까? 감사 –

관련 문제