zoukankan      html  css  js  c++  java
  • 反射做字段更新日志

    思路给类的属性加特性标签,重写savechanges方法,获取当前entry反射查看是否存在标记为通知修改做日志的属性。

     [Table("Student")]
        public partial class Student
        {
            [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
            public int id { get; set; }
    
            [StringLength(50)]
            [Notice]
            public string Name { get; set; }
    
            [Notice]
            public int Age { get; set; }
        }
    
        public class NoticeAttribute : Attribute
        { }
      public override int SaveChanges()
            {
                ObjectContext context = (this as IObjectContextAdapter).ObjectContext;
    
                foreach (ObjectStateEntry entry in context.ObjectStateManager
                          .GetObjectStateEntries(EntityState.Added | EntityState.Modified))
                {
                    var model = entry.Entity;
                    //获取类型方式1
                    var t1 = model.GetType();
                    //获取类型方式2
                    //var t2 = ObjectContext.GetObjectType(t1);
                    //获取类型方式3
                    //var ass = Assembly.GetExecutingAssembly();
                    //var name = ass.GetName();
                    //var type=ass.GetType(name.Name+ "."+t1.Name);
    
    
                    //获取当前实体所有标记为修改的属性
                    var mp = entry.GetModifiedProperties();
                    //获取类的所有属性             
                    var pro = t1.GetProperties();
                    //日志信息
                    StringBuilder log = new StringBuilder();
                    log.Append("当前用户	").Append(t1.Name).Append("	属性	");
                    foreach (var p in pro)
                    {
                        //判断类的属性是否有通知修改的特性
                        var atr = p.GetCustomAttribute(typeof(NoticeAttribute));
                        //如果属性有标记修改的特性,并且当前实体此属性标记为modified
                        if (atr != null && mp.Contains(p.Name))
                        {
                            //var value = entry.CurrentValues;
                            //var data = entry.CurrentValues.DataRecordInfo.FieldMetadata.FirstOrDefault(f=>f.FieldType.Name==p.Name);
    
                            //获取当前属性值
                            var result = p.GetValue(model);
                            log.Append(p.Name).Append("	修改为	").Append(result).Append("	");
    
                        }
    
                    }
                    Console.WriteLine(log.ToString());
    
                }
    
    
                return base.SaveChanges();
            }
      static void Main(string[] args)
            {
                Model1 db = new Model1();
                var student= db.Students.FirstOrDefault();
                student.Name = "sfdsfsfsf";
                student.Age = 18;
    
                DbEntityEntry<Student> entry = db.Entry<Student>(student);
                entry.State = System.Data.Entity.EntityState.Unchanged;
                entry.Property("Name").IsModified = true;
                entry.Property("Age").IsModified = true;
    
                db.SaveChanges();
    
                Console.ReadKey();
    
            }

  • 相关阅读:
    ApplicationContext之getBean方法详解
    Windows10终端优化方案:Ubuntu子系统+cmder+oh-my-zsh
    向 Windows 高级用户进阶,这 10 款效率工具帮你开路 | 新手问号
    Ditto —— windows 剪贴板增强小工具(复制粘贴多条记录)
    Service Mesh服务网格:是什么和为什么
    正确理解Spring事务和数据库事务和锁
    Spring中@Transactional事务回滚(含实例详细讲解,附源码)
    五分钟搞清楚MySQL事务隔离级别
    事务并发的问题场景图解
    Spring的事务管理和数据库事务相关知识
  • 原文地址:https://www.cnblogs.com/tgdjw/p/5841319.html
Copyright © 2011-2022 走看看