entity framework不查数据库修改或排除指定字段集合通用方法
其中DataDBEntities为数据库实体对象,代码如下:
下载地址:https://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
关注下面二维码,订阅更多精彩内容。

【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 推荐几款开源且免费的 .NET MAUI 组件库
· 易语言 —— 开山篇
· 实操Deepseek接入个人知识库
· Trae初体验