1. 程式人生 > >EF只更新變化的欄位

EF只更新變化的欄位

原文: EF只更新變化的欄位

摘要

在使用EF的時候,由於表字段較多,所以在更新的時候,想要只更新變化的欄位,有沒有辦法呢?

解決辦法

程式碼片段

     public async Task<int> UpdateAsync(T entity, List<string> fieldNames)
        {
            using (var context = new RetailContext())
            {

                if (fieldNames != null && fieldNames.Count > 0
) { context.Set<T>().Attach(entity); foreach (var item in fieldNames) { context.Entry<T>(entity).Property(item).IsModified = true; } }
else { context.Entry<T>(entity).State = System.Data.Entity.EntityState.Modified; } return await context.SaveChangesAsync(); } }

將變化的欄位名稱放在集合中,並修改其是否變化的狀態。

        public async Task<int> UpdateAsync(T entity, Dictionary<string
, object> dic) { try { if (dic != null) { SetValue<T>(dic, entity); return await _baseData.UpdateAsync(entity, dic.Keys.ToList()); } else { return await _baseData.UpdateAsync(entity, null); } } catch (Exception ex) { throw ex; } }
    /// <summary>
        /// 通過反射設定值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dic"></param>
        /// <param name="entity"></param>
        public static void SetValue<T>(Dictionary<string, object> dic, T entity) where T : class ,new()
        {
            Type t = entity.GetType();
            PropertyInfo[] properties = t.GetProperties();
            foreach (var item in properties)
            {
                foreach (var key in dic.Keys)
                {
                    if (key.ToLower() == item.Name.ToLower())
                    {
                        switch (item.PropertyType.ToString())
                        {
                            case "System.Int32":
                                item.SetValue(entity, Convert.ToInt32(dic[key]), null);
                                break;
                            case "System.Boolean":
                                item.SetValue(entity, Convert.ToBoolean(dic[key]), null);
                                break;
                            case "System.String":
                                item.SetValue(entity, Convert.ToString(dic[key]), null);
                                break;
                            case "System.Decimal":
                                item.SetValue(entity, Convert.ToDecimal(dic[key]), null);
                                break;
                            case "System.DateTime":
                                item.SetValue(entity, Convert.ToDateTime(dic[key]), null);
                                break;
                            case "System.Guid":
                                Guid g = dic[key] == null ? new Guid() : new Guid(dic[key].ToString());
                                item.SetValue(entity, g, null);                                
                                break;
                            default:
                                item.SetValue(entity, dic[key], null);
                                break;
                        }

                    }
                }
            }
        }

通過反射的方式對變化的欄位進行賦值。欄位中儲存變化的欄位名稱與值。