• Entity Framework with MySQL 学习笔记一(拦截)


    参考 : http://msdn.microsoft.com/en-us/data/dn469464.aspx

    EF 允许我们在发送SQL请求和返回数据时做一些拦截的动作

    比如可以自定义写 log ,修改command , 修改result 等等

    这里只是给一个简单的例子,以后有用到才研究吧.

        public class EFInterceptorForSetTimeZone : IDbCommandInterceptor 
        {       
            public void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
            {
               
            }
            public void NonQueryExecuted( DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
            {
                
            }
            public void ReaderExecuting( DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
            {
                command.CommandText = "set time_zone = '-8:00';" + command.CommandText; 
            }
            public void ReaderExecuted(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
            {
                //use table to load result then modify then convert back to reader
                var dataTable = new DataTable();
                dataTable.Load(interceptionContext.Result);
                dataTable.Rows[0]["id"] = 55555;
                interceptionContext.Result = dataTable.CreateDataReader();
            }
    
            public void ScalarExecuting( DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
            {
               
            }
            public void ScalarExecuted(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
            {
              
            }
            private void LogIfNonAsync<TResult>(DbCommand command, DbCommandInterceptionContext<TResult> interceptionContext)
            {
                
            }
            private void LogIfError<TResult>(DbCommand command, DbCommandInterceptionContext<TResult> interceptionContext)
            {
               
            } 
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                using (DB db = new DB())
                {
                    DbInterception.Add(new EFInterceptorForSetTimeZone()); //添加一个拦截器
    
                    //拦截for写log, 放一个委托函数就可以了,
                    //msg 就是entity pass 进来的 string 
                    //function (msg) { do something with no return.. };
                    db.Database.Log = msg =>
                    {
                        string y = msg;
                    };
                    var xyz = db.admins.ToList();
                }
            }
            catch (Exception ex)
            {
                string x = ex.Message; 
            }
        }
  • 相关阅读:
    学习进度报告2021/4/5
    学习进度报告2021/4/4
    学习进度报告2021/4/3
    学习进度报告2021/4/2
    学习进度报告2021/4/1
    学习进度报告2021/3/31
    《学会提问》读书笔记3
    学习进度报告2021/3/30
    MySQL入门——如何进行主从配置
    MySQL入门详解——事务、锁、优化
  • 原文地址:https://www.cnblogs.com/keatkeat/p/4112607.html
Copyright © 2020-2023  润新知