日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

entity framework不查数据库修改或排除指定字段集合通用方法

發(fā)布時間:2025/3/11 数据库 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 entity framework不查数据库修改或排除指定字段集合通用方法 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

其中DataDBEntities為數(shù)據(jù)庫實體對象,代碼如下:

下載地址: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 不查數(shù)據(jù)庫修改信息/// <summary>/// 不查數(shù)據(jù)庫修改信息/// </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){ // 參數(shù)有誤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); // 手動設(shè)置為修改狀態(tài) }else{ // 部分字段操作var _stateEntry = db.ObjectStateManager.GetObjectStateEntry(entity); // 得到實體狀態(tài)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)] 根據(jù)特性判斷主鍵EdmScalarPropertyAttribute _edmScalarPropertyAttribute =
              Attribute.GetCustomAttribute(item, typeof(EdmScalarPropertyAttribute)) as EdmScalarPropertyAttribute;if (null == _edmScalarPropertyAttribute || _edmScalarPropertyAttribute.EntityKeyProperty){ // 為主鍵或者導(dǎo)航屬性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>/// 不查數(shù)據(jù)庫修改信息/// </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){ // 參數(shù)有誤return 0;}Type _type = typeof(T);db.AttachTo(_type.Name, entity);db.ObjectStateManager.ChangeObjectState(entity, System.Data.EntityState.Modified); // 手動設(shè)置為修改狀態(tài)return db.SaveChanges();}/// <summary>/// 不查數(shù)據(jù)庫修改信息/// </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){ // 參數(shù)有誤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); // 手動設(shè)置為修改狀態(tài) }else{ // 部分字段操作var _stateEntry = db.ObjectStateManager.GetObjectStateEntry(entity); // 得到實體狀態(tài)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)] 根據(jù)特性判斷主鍵EdmScalarPropertyAttribute _edmScalarPropertyAttribute =
                Attribute.GetCustomAttribute(item, typeof(EdmScalarPropertyAttribute)) as EdmScalarPropertyAttribute;if (null == _edmScalarPropertyAttribute || _edmScalarPropertyAttribute.EntityKeyProperty){ // 為主鍵或者導(dǎo)航屬性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>/// 不查數(shù)據(jù)庫修改信息/// </summary>/// <typeparam name="T"></typeparam>/// <param name="entity"></param>/// <returns></returns>public static int Update<T>(T entity){if (null == entity){ // 參數(shù)有誤return 0;}using (DataDBEntities db = new DataDBEntities()){Type _type = typeof(T);db.AttachTo(_type.Name, entity);db.ObjectStateManager.ChangeObjectState(entity, System.Data.EntityState.Modified); // 手動設(shè)置為修改狀態(tài)return db.SaveChanges();}}#endregion}#region 修改時字段處理枚舉 /// <summary> /// 修改時字段處理枚舉 /// </summary> public enum EnumUpdateFiledType {字段修改 = 1,字段忽略 = 2 } #endregion

?

?

總結(jié)

以上是生活随笔為你收集整理的entity framework不查数据库修改或排除指定字段集合通用方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。