• 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
  • 相关阅读:
    全局变量-静态变量
    System.Web.Optimization 找不到引用,教你如何解决?
    CSS text-decoration 属性
    HTML 5 <span> 标签
    [VS]
    C# 条件表达式max=(a>b)?a:b;含义
    vs 2017 Integrated Security 为sspi 含义
    Visual studio 利用Nuget 控制台安装已经下载好的插件
    使用INTERSECT运算符
    Oracle DB 使用子查询来解决查询
  • 原文地址:https://www.cnblogs.com/vipstone/p/2750130.html
Copyright © 2020-2023  润新知