2012-12-20 25 views
11

我需要在所有DateTime和DateTime上使用Sql Server的「datetime2」類型嗎?所有實體對象的屬性。這通常是通過這樣的流暢的API來完成:更聰明的實體框架Codefirst流暢的API

modelBuilder.Entity<Mail>().Property(c => c.SendTime).HasColumnType("datetime2"); 

不過,我不希望爲每一個DateTime字段中的每一個實體型手動完成。 (我沒有可以放置所有DateTime屬性的公共基類型,因爲DateTime屬性特定於它們所在的實體類型)。

簡短的問題:我有什麼選擇?

長期以來的問題:我正在考慮使用反射並做了一個嘗試,但它看起來非常混亂,因爲流暢的API並不適合這種工作,因爲我不得不通過通用調用API方法反射。這裏是我凌亂的嘗試:

protected override void OnModelCreating(DbModelBuilder modelBuilder) { 
     var genericEntityMethod = modelBuilder.GetType().GetMethod("Entity"); 
     var entityTypes = Assembly.GetExecutingAssembly().GetTypes().Where(t => t.IsClass && !t.IsAbstract && t.GetInterface("IEntity") != null); 
     foreach (var t in entityTypes) { 
      var props = t.GetProperties().Where(p => 
       p.GetSetMethod() != null && 
       p.GetGetMethod() != null && 
       (p.PropertyType == typeof (DateTime) || 
       p.PropertyType == typeof(DateTime?))); 
      foreach (var propertyInfo in props) { 
       var entityMethod = genericEntityMethod.MakeGenericMethod(t.GetType()); 
       var entityTypeConfiguration = entityMethod.Invoke(modelBuilder,null); 
       var lambdaExpression = Expression.Lambda(Expression.MakeMemberAccess(Expression.Parameter(t), propertyInfo), Expression.Parameter(t)); 

       //var propertyMethod = entityTypeConfiguration.GetType().GetMethod("Property"); // Cant get this to work 
       var propertyMethods = entityTypeConfiguration.GetType().GetMethods().Where(m => m.ReturnType == typeof(DateTimePropertyConfiguration)).ToList(); 
       var propertyMethod = propertyInfo.PropertyType == typeof (DateTime) ? propertyMethods[0] : propertyMethods[1]; 

       var dateTimePropertyConfiguration = propertyMethod.Invoke(entityTypeConfiguration, new object[] {lambdaExpression}); 
       var hasColumnTypeMethod = entityTypeConfiguration.GetType().GetMethod("HasColumnType"); 
       hasColumnTypeMethod.Invoke(dateTimePropertyConfiguration, new object[]{ "datetime2" }); 
      } 
     } 
     base.OnModelCreating(modelBuilder); 
    } 

這種失敗在這行:

    var dateTimePropertyConfiguration = propertyMethod.Invoke(entityTypeConfiguration, new object[] {lambdaExpression}); 

此錯誤(Entities.Order是具有一個DateTime屬性我的實體對象之一):

Object of type 'System.Linq.Expressions.Expression`1[System.Func`2[Entities.Order,System.Nullable`1[System.DateTime]]]' cannot be converted to type 'System.Linq.Expressions.Expression`1[System.Func`2[System.RuntimeType,System.Nullable`1[System.DateTime]]] 

任何人都可以用這種反射方法或更好的幫助我,讓我更簡單的方法來避免手動編寫大量流利的API?

回答

4

還有好得多的做法。不幸的是,這種方法是基於EF6中的習慣慣例。您可以嘗試EF6 Alpha 2並自定義約定 - walkthrough包含您具體要求的示例。

要解決您的反射問題,請檢查此blog article。它描述了用反射生成映射的代碼(應該有相關的open source project)。

相關問題