2015年1月20日 星期二

Entity Framework Code First依屬性(Attribute)設定SQL欄位 (續)

花了點時間把Entity Framework Code First依屬性(Attribute)設定SQL欄位的程式碼Review過一次,總覺得設定的地方怪怪的,GetMethods()以後,指定第幾個到底是為了什麼?

if (propAttr.prop.PropertyType.IsGenericType && propAttr.prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
  methodInfo = entityConfig.GetType().GetMethods().Where(p => p.Name == "Property").ToList()[7];
}
else
{
  methodInfo = entityConfig.GetType().GetMethods().Where(p => p.Name == "Property").ToList()[6];
}
decimalConfig = methodInfo.Invoke(entityConfig, new[] { lambdaExpression }) as DecimalPropertyConfiguration;
decimalConfig.HasPrecision((byte)propAttr.attr.Precision, (byte)propAttr.attr.Scale);

從Debug模式下去看,原來Property有很多,需要指定正確的Type,不然就會發生轉換失敗的錯誤,而部分資料類別又有區分允不允許NULL,所以造就了上面詭異的CODE
判斷有沒有允許NULL後,強制指定對應的資料型別,這個方法在網路上廣為流傳,但這實在是太暴力了,如果哪一天順序被改掉,會有一堆人死在那邊
為了避免這種情況發生,決定嘗試去判斷對應型別有沒有允許NULL,無奈小弟功力不夠深厚,一直找不出解決方法

運氣還不錯的找到了一組原始碼HerrKater/EntityHelper.cs
參考了裡面的ProcessDecimalProperties,在GetMethod取得MethodInfo時,後面傳入的Type[]使用LambdaExpression.GetType()去指定對應型別,這時候就可以找到我們正確的DecimalPropertyConfiguration
MethodInfo methodInfo = entityConfig.GetType().GetMethod("Property", new[] { lambdaExpression.GetType() });
DecimalPropertyConfiguration decimalConfig = methodInfo.Invoke(entityConfig, new[] { lambdaExpression }) as DecimalPropertyConfiguration;
decimalConfig.HasPrecision((byte)propAttr.attr.Precision, (byte)propAttr.attr.Scale);


另外參考了他取得在DbContext內宣告DbSet<>的方法
var contextProperties = typeof (T).GetProperties().Where(p => p.PropertyType.IsGenericType && p.PropertyType.GetGenericTypeDefinition() == typeof (DbSet<>));

決定把原本用命名空間做判斷的方式改掉,但是他是用PropertyInfo,然後在迴圈裡面進行轉換,這樣會變更到原本我們的Code,所以做一些調整
var tmplist = typeof(T).GetProperties().Where(p => p.PropertyType.IsGenericType && p.PropertyType.GetGenericTypeDefinition() == typeof(DbSet<>)).SelectMany(p => p.PropertyType.GetGenericArguments()).Where(t => t.GetProperties(BindingFlags.Public | BindingFlags.Instance).Any(p => p.GetCustomAttribute<DecimalAttribute>() != null));

備註:
取得PropertyInfo以後,要先取PropertyType找出宣告的Type,但是因為有外層的DbSet<>,所以還需要再使用GetGenericArguments()去找出定義的Model

參考:
Entity Framework Code First建立Decimal,並設定大小
Entity Framework Code First依屬性(Attribute)設定SQL欄位
HerrKater/EntityHelper.cs

2015年1月19日 星期一

Entity Framework Code First依屬性(Attribute)設定SQL欄位

繼上遍Entity Framework Code First建立Decimal,並設定大小,覺得要針對每個Model設定SQL欄位大小,又要設定驗證大小,日後維護要改2個地方,有點給他麻煩,甚至容易忘記

嘗試對已有設定DecimalAttribute的Model在OnModelCreating的時候,把所有的都一次設定完

首先建立一個靜態類別DecimalModelCreating,並建立設定方法OnModelCreating(如下)


public static class DecimalModelCreating
{
  public static void OnModelCreating(DbModelBuilder modelBuilder, string Namespace)
  {
    //取得所有在指定命名空間中,有欄位包含DecimalAttribute的類別
    foreach (Type classType in from t in Assembly.GetAssembly(typeof(DecimalAttribute)).GetTypes() where t.IsClass == true && string.IsNullOrEmpty(t.Namespace) == false && t.Namespace.Equals(Namespace, StringComparison.CurrentCultureIgnoreCase) == true && t.GetProperties(BindingFlags.Public | BindingFlags.Instance).Any(p => p.GetCustomAttribute<DecimalAttribute>() != null) select t)
    {
      //取得類別中,包含DecimalAttribute的欄位
      foreach (var propAttr in classType.GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(p => p.GetCustomAttribute<DecimalAttribute>() != null).Select(p => new { prop = p, attr = p.GetCustomAttribute<DecimalAttribute>(true) }))
      {
        var entityConfig = modelBuilder.GetType().GetMethod("Entity").MakeGenericMethod(classType).Invoke(modelBuilder, null);
        ParameterExpression param = ParameterExpression.Parameter(classType, "c");
        Expression property = Expression.Property(param, propAttr.prop.Name);
        LambdaExpression lambdaExpression = Expression.Lambda(property, true, new ParameterExpression[] { param });
        DecimalPropertyConfiguration decimalConfig;
        MethodInfo methodInfo;
        //依欄位是否允許null,設定對應MethodInfo
        if (propAttr.prop.PropertyType.IsGenericType && propAttr.prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
        {
          methodInfo = entityConfig.GetType().GetMethods().Where(p => p.Name == "Property").ToList()[7];
        }
        else
        {
          methodInfo = entityConfig.GetType().GetMethods().Where(p => p.Name == "Property").ToList()[6];
        }
        decimalConfig = methodInfo.Invoke(entityConfig, new[] { lambdaExpression }) as DecimalPropertyConfiguration;
        decimalConfig.HasPrecision((byte)propAttr.attr.Precision, (byte)propAttr.attr.Scale);
      }
    }
  }
}

接著將DbContextOnModelCreating調整為使用DecimalModelCreating進行設定
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
  DecimalModelCreating.OnModelCreating(modelBuilder, "YourNamespance");
  base.OnModelCreating(modelBuilder);
}

參考:
Entity Framework Code First建立Decimal,並設定大小

2015/01/19調整:
近期將部分資料表移到Azure Storage Table的時候,發生了資料庫移轉錯誤
在模型產生期間偵測到一個或多個驗證錯誤:
System.Data.Entity.Edm.EdmEntityType: : EntityType 'myclass' 未定義索引鍵。請定義此 EntityType 的索引鍵。
System.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'myclass' 是以未定義索引鍵的類型 'myclass' 為基礎。

檢查OnModelCreating後發現,在條件中,只要是在指定命名空間(Namespace)的Model都會被設定:
t.Namespace.Equals(Namespace, StringComparison.CurrentCultureIgnoreCase) == true

增加判斷語法,除了命名空間以外,還要有宣告在DbContext裡面:
typeof(MyContext).GetProperties().Where(x => x.PropertyType.IsGenericType && x.PropertyType.Name.StartsWith("DbSet") && x.PropertyType.GenericTypeArguments != null && x.PropertyType.GenericTypeArguments.Count() > 0).SelectMany(x => x.PropertyType.GenericTypeArguments).Select(x => x.Name).Contains(t.Name)

參考:
Entity Framework Code First建立Decimal,並設定大小
延伸閱讀:
Entity Framework Code First依屬性(Attribute)設定SQL欄位 (續)

Entity Framework Code First建立Decimal,並設定大小

在Sql建立Decimal的時候,習慣性會設定大小,但是在使用Entity Framework Code First的時候卻發現2個問題:
1.沒辦法改變大小(預設是18,2),可能欄位太大或是小數位數不夠
2.沒辦法檢查傳入的資料有沒有在限制的範圍內,可能會發生輸入不符合規則

有人會使用float或double,然後配合[RangeAttribute]去限制大小,以及[RegularExpressionAttribute]來檢驗格式;這或許解決了資料驗證的問題,但是總不可能每個欄位都去下這些屬性,對資料庫欄位大小問題也一樣沒有解決。
首先先來解決資料庫decimal大小問題。
網路上找的最快的方式(Set decimal(16, 3) for a column in Code First Approach in EF4.3)
public class MyContext : DbContext
{
  public DbSet<myclass> MyClass;
  protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
    modelBuilder.Entity<myclass>().Property(x => x.Sum).HasPrecision(5, 1);
    modelBuilder.Entity<myclass>().Property(x => x.Price).HasPrecision(3, 1);
    base.OnModelCreating(modelBuilder);
  }
}
解決了資料庫大小問題


測試下去就會發現,輸入多少都可以過,只是到資料庫會被截掉,要再加個驗證讓ModelState.IsValid可以為我們擋掉。
先建立一個DecimalAttribute繼承ValidationAttribute
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
public class DecimalAttribute : ValidationAttribute
{
  public int Precision = 18;
  public int Scale = 2;
  public DecimalAttribute(int Precision = 18, int Scale = 2)
  {
    this.Precision = Precision;
    this.Scale = Scale;
  }
  public override bool IsValid(object value)
  {
    if (value == null)
    {
      return true;
    }
    try
    {
      decimal valueAsDecimal = Convert.ToDecimal(value);
      SqlDecimal sqlDecimal = SqlDecimal.ConvertToPrecScale(new SqlDecimal(valueAsDecimal), Precision, Scale);
    }
    catch (Exception ex)
    {
      if (string.IsNullOrEmpty(base.ErrorMessage) == true)
      {
        base.ErrorMessage = ex.Message;
      }
      return false;
    }
    return true;
  }
}
傳入資料測試驗證機制正常運行

延伸閱讀:
Entity Framework Code First依屬性(Attribute)設定SQL欄位
Entity Framework Code First依屬性(Attribute)設定SQL欄位 (續)