entity framework不查数据库修改或排除指定字段集合通用方法
生活随笔
收集整理的這篇文章主要介紹了
entity framework不查数据库修改或排除指定字段集合通用方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
其中DataDBEntities為數據庫實體對象,代碼如下:
下載地址:http://files.cnblogs.com/stone_w/EFDBHelper.zip
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; using System.Data.Objects.DataClasses; public class EFDBHelper {#region 不查數據庫修改信息/// <summary>/// 不查數據庫修改信息/// </summary>/// <typeparam name="T"></typeparam>/// <param name="entity"></param>/// <param name="db"></param>/// <param name="updateFiledType"></param>/// <param name="fileds"></param>/// <returns></returns>public static int Update<T>(T entity, DataDBEntities db,EnumUpdateFiledType updateFiledType, params string[] fileds){if (null == db || null == entity){ // 參數有誤return 0;}Type _type = typeof(T);db.AttachTo(_type.Name, entity);if (null == fileds || fileds.Length == 0){ // 全字段操作db.ObjectStateManager.ChangeObjectState(entity, System.Data.EntityState.Modified); // 手動設置為修改狀態 }else{ // 部分字段操作var _stateEntry = db.ObjectStateManager.GetObjectStateEntry(entity); // 得到實體狀態if (EnumUpdateFiledType.字段修改 == updateFiledType){ // 部分字段修改for (int i = 0; i < fileds.Length; i++){_stateEntry.SetModifiedProperty(fileds[i]);}}else{ // 部分字段排除PropertyInfo[] _properties = _type.GetProperties(); // 得到類的所有屬性foreach (PropertyInfo item in _properties){if ("EntityState" == item.Name || "EntityKey" == item.Name){continue;}// 主鍵判斷 [EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)] 根據特性判斷主鍵EdmScalarPropertyAttribute _edmScalarPropertyAttribute =Attribute.GetCustomAttribute(item, typeof(EdmScalarPropertyAttribute)) as EdmScalarPropertyAttribute;if (null == _edmScalarPropertyAttribute || _edmScalarPropertyAttribute.EntityKeyProperty){ // 為主鍵或者導航屬性continue;}bool _thisIsUpdateFiled = true; // 是否為修改字段for (int i = 0; i < fileds.Length; i++){if (item.Name == fileds[i]){_thisIsUpdateFiled = false;break;}}if (_thisIsUpdateFiled)_stateEntry.SetModifiedProperty(item.Name);}}}return db.SaveChanges();}/// <summary>/// 不查數據庫修改信息/// </summary>/// <typeparam name="T"></typeparam>/// <param name="entity"></param>/// <param name="db"></param>/// <returns></returns>public static int Update<T>(T entity, DataDBEntities db){if (null == db || null == entity){ // 參數有誤return 0;}Type _type = typeof(T);db.AttachTo(_type.Name, entity);db.ObjectStateManager.ChangeObjectState(entity, System.Data.EntityState.Modified); // 手動設置為修改狀態return db.SaveChanges();}/// <summary>/// 不查數據庫修改信息/// </summary>/// <typeparam name="T"></typeparam>/// <param name="entity"></param>/// <param name="updateFiledType"></param>/// <param name="fileds"></param>/// <returns></returns>public static int Update<T>(T entity, EnumUpdateFiledType updateFiledType, params string[] fileds){if (null == entity){ // 參數有誤return 0;}using (DataDBEntities db = new DataDBEntities()){Type _type = typeof(T);db.AttachTo(_type.Name, entity);if (null == fileds || fileds.Length == 0){ // 全字段操作db.ObjectStateManager.ChangeObjectState(entity, System.Data.EntityState.Modified); // 手動設置為修改狀態 }else{ // 部分字段操作var _stateEntry = db.ObjectStateManager.GetObjectStateEntry(entity); // 得到實體狀態if (EnumUpdateFiledType.字段修改 == updateFiledType){ // 部分字段修改for (int i = 0; i < fileds.Length; i++){_stateEntry.SetModifiedProperty(fileds[i]);}}else{ // 部分字段排除PropertyInfo[] _properties = _type.GetProperties(); // 得到類的所有屬性foreach (PropertyInfo item in _properties){if ("EntityState" == item.Name || "EntityKey" == item.Name){continue;}// 主鍵判斷 [EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)] 根據特性判斷主鍵EdmScalarPropertyAttribute _edmScalarPropertyAttribute =
Attribute.GetCustomAttribute(item, typeof(EdmScalarPropertyAttribute)) as EdmScalarPropertyAttribute;if (null == _edmScalarPropertyAttribute || _edmScalarPropertyAttribute.EntityKeyProperty){ // 為主鍵或者導航屬性continue;}bool _thisIsUpdateFiled = true; // 是否為修改字段for (int i = 0; i < fileds.Length; i++){if (item.Name == fileds[i]){_thisIsUpdateFiled = false;break;}}if (_thisIsUpdateFiled)_stateEntry.SetModifiedProperty(item.Name);}}}return db.SaveChanges();}}/// <summary>/// 不查數據庫修改信息/// </summary>/// <typeparam name="T"></typeparam>/// <param name="entity"></param>/// <returns></returns>public static int Update<T>(T entity){if (null == entity){ // 參數有誤return 0;}using (DataDBEntities db = new DataDBEntities()){Type _type = typeof(T);db.AttachTo(_type.Name, entity);db.ObjectStateManager.ChangeObjectState(entity, System.Data.EntityState.Modified); // 手動設置為修改狀態return db.SaveChanges();}}#endregion}#region 修改時字段處理枚舉 /// <summary> /// 修改時字段處理枚舉 /// </summary> public enum EnumUpdateFiledType {字段修改 = 1,字段忽略 = 2 } #endregion
?
?
總結
以上是生活随笔為你收集整理的entity framework不查数据库修改或排除指定字段集合通用方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: T-Sql备份还原数据库
- 下一篇: Net操作Excel(终极方法NPOI)