• C#获取EF实体对象或自定义属性类的字段名称和值


    在年前上班的时候遇到了一个问题是这样描述的:我前台设计一个页面,是标签和文本框,当用户修改了哪个文本框的值,将该修改前的值、修改后的值,该值对应的字段,该值对应的行id获取到保存到数据库的某张表里。现在数据库有一张表,结构如下:

    修改记录表
    ID   修改行ID 修改列名称 修改前的值 修改后的值 所属修改批次 修改时间
    1   1 test 10 11 2013-10-11 第一批次 2013-10-11

    思路:

    1、将数据封装成实体对象传到后台。整套下来是用MVC的机制实现的

    2、利用反射。获取对象的字段名称、修改前的值、修改后的值

    3、调用保存方法。将修改记录保存到数据库。

    4、下面贴上主要实现代码,里面包含设计实现思路:

     1             //判断字段是否修改(原值、旧值做比较),student为前台传入的数据对象
     2             var oldModel = this.db.Students.Where(p => p.ID == student.ID).FirstOrDefault();
     3             if (oldModel == null)
     4             {
     5                 return "返回数据为空!";
     6             }
     7 
     8             //获取当前对象的属性数组
     9             PropertyInfo[] currentPro = student.GetType().GetProperties();
    10             //获取原始对象的属性数组
    11             PropertyInfo[] oldPro = oldModel.GetType().GetProperties();
    12             foreach (PropertyInfo old in oldPro)
    13             {
    14                 foreach (PropertyInfo curtrent in currentPro.
    15                     Where(p => !string.IsNullOrEmpty(p.Name))
    16                 {
    17                     //获取当前数据
    18                     object currentColumnValue = curtrent.GetValue(student, null);
    19                     //获取原始数据
    20                     object oldColumnValue = old.GetValue(oldModel, null);
    21 
    22                     //判断值是否相等
    23                     if (currentColumnValue.ToString() != oldColumnValue.ToString())
    24                     {
    25                         //保存新值/旧值到数据库
    26                         SaveStudentLog(oldModel.ID, oldColumnValue.ToString(), currentColumnValue.ToString(), old.Name);
    27                         break;
    28                     }
    29                 }
    30             }
    通过反射获取对象包含的字段名称和值
     1  private void SaveStudentLog(int id, string oldValue, string currentValue, string column)
     2         {
     3             StudentLog studentLog = new StudentLog();
     4             var student = this.db.Students.Where(p => p.ID == id).FirstOrDefault();
     5             if (student == null)
     6             {
     7                 return;
     8             }
     9 
    10             studentLog.Student = student;
    11             studentLog.ValueOfOld = oldValue.Trim();
    12             studentLog.ValueOfNew = currentValue.Trim();
    13             studentLog.FieldName = column;
    14             studentLog.FieldDesc = "1223";
    15             studentLog.UserAccount = "123";
    16             studentLog.UserRoleName = "34";
    17             studentLog.UserTrueName = "123";
    18             studentLog.UpdateDesc = "123";
    19             studentLog.UpdateTime = DateTime.Now;
    20             studentLog.UpdateTimeTag = DateTime.Now.Hour.ToString() +
    21                                        DateTime.Now.Minute.ToString() +
    22                                        DateTime.Now.Second.ToString() +
    23                                        DateTime.Now.Millisecond.ToString();
    24             this.db.StudentLogs.AddObject(studentLog);
    25             this.db.SaveChanges();
    26         }
    保存记录到数据库

    这样下来,通过调用一个保存编辑函数SaveStudentLog(行ID,修改前的值,修改后的值,列名)即可将修改的值信息保存到记录表里。

  • 相关阅读:
    EasyUI应用总结
    ExcelUtil
    搭建Easyui环境在Myeclipse或Eclipse中
    Easyui Datagrid 如何实现后台交互显示用户数据列表
    mybatis整合ehcache
    Flynn初步:基于Docker的PaaS台
    Following unknown configure options were used:--enable-fpm
    Android决议具体解释
    cocos2dx lua
    Android 建立View 圆角
  • 原文地址:https://www.cnblogs.com/AlphaThink-AT003/p/3540464.html
Copyright © 2020-2023  润新知