博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

EF修改前修改后字段的值

Posted on 2012-09-14 10:09  itcfj  阅读(468)  评论(0编辑  收藏  举报
 1         /// <summary>
 2         /// 获取修改拼接字符串
 3         /// </summary>
 4         /// <returns></returns>
 5         public string GetModifyStr()
 6         {
 7             string str = string.Empty;
 8             ObjectStateEntry[] objModified = (ObjectStateEntry[])DataContext
 9                     .ObjectStateManager
10                     .GetObjectStateEntries(System.Data.EntityState.Modified);
11             ObjectStateEntry entry = objModified.Where(P => P.Entity.GetType().ToString() == "BF.Model.res_HumanResources").FirstOrDefault();
12             List<string> fieldNames = entry.GetModifiedProperties().ToList();
13             foreach (var fieldName in fieldNames)
14             {
15                 if (!entry.CurrentValues[fieldName].Equals(entry.OriginalValues[fieldName]))
16                 {
17                     dic_Dict dic = DataContext.dic_Dict.Where(P => P.sTableCode == "HumanResources" && P.sCode == fieldName).FirstOrDefault();
18                     if (dic != null)
19                     {
20                         string sName = dic.sName;
21                         string oldStr = entry.OriginalValues[fieldName].ToString();
22                         oldStr = String.IsNullOrEmpty(oldStr) ? "[空]" : FiledName(fieldName, oldStr);
23                         string newStr = entry.CurrentValues[fieldName].ToString();
24                         newStr = String.IsNullOrEmpty(newStr) ? "[空]" : FiledName(fieldName, newStr);
25                         str += string.Format(" {0}({1}): {2}=>{3}; ", sName, fieldName, oldStr, newStr);
26                     }
27                 }
28             }
29             return str;
30         }
31         /// <summary>
32         ///33         /// </summary>
34         /// <returns></returns>
35         public string GetModifyStr(string fieldName)
36         {
37             string str = string.Empty;
38             ObjectStateEntry[] objModified = (ObjectStateEntry[])DataContext
39                     .ObjectStateManager
40                     .GetObjectStateEntries(System.Data.EntityState.Modified);
41             ObjectStateEntry entry = objModified.Where(P => P.Entity.GetType().ToString() == "BF.Model.res_HumanResources").FirstOrDefault();
42             List<string> fieldNames = entry.GetModifiedProperties().ToList();
43             if (!entry.CurrentValues[fieldName].Equals(entry.OriginalValues[fieldName]))
44             {
45                 dic_Dict dic = DataContext.dic_Dict.Where(P => P.sTableCode == "HumanResources" && P.sCode == fieldName).FirstOrDefault();
46                 if (dic != null)
47                 {
48                     string sName = dic.sName;
49                     string oldStr = entry.OriginalValues[fieldName].ToString();
50                     oldStr = String.IsNullOrEmpty(oldStr) ? "[空]" : FiledName(fieldName, oldStr);
51                     string newStr = entry.CurrentValues[fieldName].ToString();
52                     newStr = String.IsNullOrEmpty(newStr) ? "[空]" : FiledName(fieldName, newStr);
53                     str = string.Format(" {0}({1}): {2}=>{3}; ", sName, fieldName, oldStr, newStr);
54                 }
55             }
56 
57             return str;
58         }
59 
60