2013-07-11 25 views
0

我想在EntityTypeConfiguration上做一個通用的擴展方法<T>這將允許我枚舉T上的所有字符串屬性併爲它們設置IsUnicode(false)。這是我迄今爲止,但我堅持試圖獲得StringPropertyConfiguration,這個類的構造函數是內部的,我不知道在哪裏獲得它的一個實例。重寫IsUnicode使用通用擴展

public static void SetStringsToBeNonUnicode<T>(this EntityTypeConfiguration<T> config) where T : class 
{ 

    PropertyInfo[] properties = typeof (T).GetProperties(); 
    foreach (PropertyInfo p in properties) 
    { 
     var parameter = Expression.Parameter(typeof(T)); 
     var property = Expression.Property(parameter, p); 
     var funcType = typeof(Func<,>).MakeGenericType(typeof(T), p.PropertyType); 
     var lambda = Expression.Lambda(funcType, property, parameter); 
     //This is the line where I need help 
     StringPropertyConfiguration stringConfig = 
      new System.Data.Entity.ModelConfiguration.Configuration.StringPropertyConfiguration(config.Property<System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive.StringPropertyConfiguration>((LambdaExpression) property));      
     stringConfig.IsUnicode(false); 

    } 
} 

回答

0

我知道這是爲時已晚,但如果其他人需要它:

((StringPropertyConfiguration)config.GetType().GetMethod("Property", new Type[] { lambda.GetType() }).Invoke((object)config, new object[] { lambda })).IsUnicode(false); 
+0

這是從來沒有遲到,如果東西工程:) EF6已經解決了這一點,但是任何人使用舊版本可以試試這個卡 – BlackICE