使用SaveChanging事件

1、使用ADO.Net Entity Data Model 和Linq to Sql 建立数据访问层,SqlBtmsModel 为 生成的ObjectContext 实现类或者DataContext 的实现类

2、编写代码 实现 ObjectContext 或者 DataContext 的 分部定义

partial class SqlBtmsModel
    {

        public static Action<SqlBtmsModel> RecordHandel;
        private bool logFlag = true;
        public SqlBtmsModel(bool logFlag) :
            base("name=SqlBtmsModel", "SqlBtmsModel")
        {
            this.logFlag = logFlag;
            OnContextCreated();
        }

        partial void OnContextCreated()
        {
            if (logFlag)
                this.SavingChanges += new EventHandler(Context_SavingChanges);
        }

        private void Context_SavingChanges(object sender, EventArgs e)
        {
            SqlBtmsModel context = sender as SqlBtmsModel;
            RecordHandel(context);
        }
    }

3、定义日志类

public class SqlLogManager
    {

 public static void SaveToDB(SqlBtmsModel context)
        {
            foreach (ObjectStateEntry entry in
                context.ObjectStateManager.GetObjectStateEntries(
                EntityState.Added | EntityState.Modified | EntityState.Deleted))
            {
                string currentValues = "", originalValues = "";
                switch (entry.State)
                {

                    case EntityState.Added:
                        currentValues = "";
                        foreach (string item in entry.GetModifiedProperties())
                        {
                            currentValues += " |" + item + ":" + entry.CurrentValues[item];
                        }

                        Save(context, EntityState.Added, entry.EntitySet.Name, null, currentValues);
                        break;
                    case EntityState.Modified:
                        currentValues = ""; originalValues = "";
                        foreach (string item in entry.GetModifiedProperties())
                        {
                            originalValues += " |" + item + ":" + entry.OriginalValues[item];
                            currentValues += " |" + item + ":" + entry.CurrentValues[item];
                        }
                        Save(context, EntityState.Modified, entry.EntitySet.Name, originalValues, currentValues);
                        break;
                    case EntityState.Deleted:
                        originalValues = "";
                        foreach (string item in entry.GetModifiedProperties())
                        {
                            originalValues += " |" + item + ":" + entry.OriginalValues[item];
                        }

                        Save(context, EntityState.Deleted, entry.EntitySet.Name, originalValues, null);
                        break;
                }

            }
        }
        private static void Save(SqlBtmsModel context, EntityState operationType, string entitySetName, string originalValues, string currentValues)
        {
            SqlBtmsModel logContext = new SqlBtmsModel(false);
            OperationRecords record = new OperationRecords();
            UserInfo userInfo = UserInfo.GetUserInfo();
            record.SeqNo = Guid.NewGuid();
            record.OperateDate = DateTime.Now;
            record.UserId = userInfo.User.UserName;
            record.UserName = userInfo.User.AliasName;
            record.Org = userInfo.User.Org.Code + userInfo.User.Org.HostId;
            record.OperateType = operationType.ToString();
            record.ResourceName = entitySetName;
            record.CurrentData = currentValues;
            record.OriginalData = originalValues;
            logContext.AddToOperationRecords(record);
            logContext.SaveChanges();

        }

}

4、在程序开始的时候,进行关联

SqlBtmsModel.RecordHandel = SqlLogManager.SaveToDB;

5、以后使用Using(SqlBtmsModel context=new SqlBtmsModel()){.......}或者 Using(SqlBtmsModel context=new SqlBtmsModel(true)){.......}进行数据库的增、删、修改操作时,将自动记录日志,且每操作一条数据,便记录一条日志。若不想记录日志,则可以使用Using(SqlBtmsModel context=new SqlBtmsModel(false)){.......}

阅读更多
个人分类: Entity FrameWork
想对作者说点什么? 我来说一句

C# 代理 委托 事件 反射.pdf

2013年01月15日 482KB 下载

TUXEDO订阅系统事件

2012年08月15日 28KB 下载

域、属性与事件

2011年09月10日 211KB 下载

VC++使用事件启动和终止进程

2011年12月25日 14KB 下载

百度统计图解文档

2018年01月10日 940KB 下载

Labview使用事件的对话框

2015年08月28日 12KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭