zoukankan      html  css  js  c++  java
  • 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; 
            }
        }
  • 相关阅读:
    解决com.xpand.. starter-canal 依赖引入问题
    缓存预热加入二级缓存
    缓存预热的实现
    ShardingSphere 中有哪些分布式主键实现方式?
    ShardingSphere 如何实现系统的扩展性
    如何系统剖析 ShardingSphere 的代码结构?
    SharingSphere的数据脱敏
    ShardingSphere的分布式事务
    Qt 事件过滤器原理(installEventFilter函数)
    Qt Event 以及 Event Filter 事件处理
  • 原文地址:https://www.cnblogs.com/keatkeat/p/4112607.html
Copyright © 2011-2022 走看看