其中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
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有