entity frame ewarkはデータベースの変更を調べず、または指定されたフィールドのセットを除外するための共通の方法です。

20273 ワード

このうちDataDB Enttiesはデータベース本体の対象となります。コードは以下の通りです。
ダウンロード住所: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