其中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不查数据库修改或排除指定字段集合通用方法相关推荐

  1. 数据库修改update更新指定字段或者所有字段

    UPDATE Table SET name = '张三', age = '55' WHERE id = '1' [使用方法讲解]                        UPDATE 表名 SE ...

  2. Entity Framework Code First添加修改及删除单独实体

    对于一个单独实体的通常操作有3种:添加新的实体.修改实体以及删除实体. 1.添加新的实体 Entity Framework Code First添加新的实体通过调用DbSet.Add()方法来实现. ...

  3. 使用Entity Framework Core访问数据库(DB2篇)

    上一篇讲了一些EF Core访问Oracle的坑.(感兴趣请移步:使用Entity Framework Core访问数据库(Oracle篇)) 这篇主要讲一下关于EF Core访问DB2的一揽子~问题 ...

  4. oracle精简版_使用Entity Framework Core访问数据库(Oracle篇)

    前言 哇..看看时间 真的很久很久没写博客了 将近一年了. 最近一直在忙各种家中事务和公司的新框架  终于抽出时间来更新一波了. 本篇主要讲一下关于Entity Framework Core访问ora ...

  5. 使用Entity Framework Core访问数据库(Oracle篇)

    前言 哇..看看时间 真的很久很久没写博客了 将近一年了. 最近一直在忙各种家中事务和公司的新框架  终于抽出时间来更新一波了. 本篇主要讲一下关于Entity Framework Core访问ora ...

  6. ruby pluck用法,可以快速从数据库获取 对象的 指定字段的集合数组

    可以快速从数据库获取 对象的 指定字段的集合数组 比如有一个users表,要等到user的id数组: select id from users where age > 20; 要实现在如上sql ...

  7. Entity Framwork(EF) 7——在Controller内获取指定字段的值

    Entity Framwork(EF) 7--在Controller内获取指定字段的值 一.开发背景: 在用户登录的时候,验证用户和密码是否正确.验证通过后将用户名和用户ID保存下来以便后续数据更新时 ...

  8. JSON排除指定字段的4种方法

    通常在 本地存储 / 微服务 / 分布式 通讯场景下,会用到对象序列化,Serializable只是一个接口类,需要具体的对象实现它.本文主要介绍序列化时(如Fastjson.Gson.Jackson ...

  9. Entity Framework Core 之数据库迁移

    前言 最近打算用.NET Core写一份开源的简易CMS系统,来练练手 所以又去深入研究了一下Entity Framework Core 发现其实有些细节园子里还是很少讲到. 特意整理了几个细节. 正 ...

最新文章

  1. ListView用法
  2. 六十八、SpringBoot连接MongoDB操作
  3. 开发日志_Jan.8.2017
  4. python一个函数调用另一个函数的返回值_在python函数中使用True,False和None作为返回值...
  5. 信息学奥赛一本通 1173:阶乘和 | OpenJudge NOI 1.6 15 | 洛谷 P1009 [NOIP1998 普及组] 阶乘之和
  6. java单例模式实例_Java设计模式之单例模式 通俗易懂 超详细 【内含案例】
  7. bootstrap设计登录页面_前端小白如何在10分钟内打造一个爆款Web响应式登录界面?...
  8. log4j配置以及logback配置
  9. 神经网络与深度学习2
  10. runTime动态给类添加属性
  11. js正则表达式校验手机号码和电话号码
  12. 建筑工程师的转行学计算机科学与技术的抉择
  13. KB,MB单位转换(Vue)
  14. 用八类网线钳和剥线刀做网线水晶头
  15. 使用SpringMVC框架实现员工管理系统
  16. python抓取汇率_09 使用Python爬取中国银行网站选择汇率最坑的一天
  17. 一位知名 Python 技术博主因病离世
  18. PCB线路板进行热设计的方法都有哪些?
  19. 深度学习入门与快速实践
  20. MySQL学习:isnull()、ifnull()、nullif()

热门文章

  1. 防火墙firewalld
  2. vue-router之路由钩子(八)
  3. 由mysql8降级到mysql5
  4. 《黃帝內經 —— 央視60集紀錄片》
  5. SUBSTR函数的使用
  6. centos 修改ip地址
  7. VSTS For Testers读书笔记(5)
  8. 设计模式的功力长了!
  9. python跨函数调用变量_对python中不同模块(函数、类、变量)的调用详解
  10. Oracle下的Databse,Instance,Schemas