首页新闻找找看学习计划

ef 通过DbCommandInterceptor 修改数据库连接 添加数据异常

0
悬赏园豆:100 [待解决问题]

我想实现数据库的读写分离 ef6里面加了DbCommandInterceptor的功能
可是修改以后 在添加新数据的时候 抛出异常 
“基础提供程序在 Commit 上失败 值不能为 null。 参数名: connection”
但是数据已经成功添加,那位前辈读到过这个问题呢

 public class EntityCommandInterceptor: DbCommandInterceptor
    {
        private string readConnstring = ConfigurationManager.ConnectionStrings["readConnection"].ToString();
        private string writeConnstring = ConfigurationManager.ConnectionStrings["writeConnection"].ToString();


        public override void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
        {
            string SqlCommand = command.CommandText.ToUpper();
            command.Connection.Dispose();
            command.Connection = new SqlConnection();
            if (SqlCommand.IndexOf("SELECT") == 0 )
            {


                command.Connection.ConnectionString = readConnstring;


            }
            else
            {
                command.Connection.ConnectionString = writeConnstring;
            }

            command.Connection.Open();




            base.ReaderExecuting(command, interceptionContext);
        }

        public override void ScalarExecuting(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
        {

            command.Connection.Dispose();
            command.Connection = new SqlConnection();

            command.Connection.ConnectionString = readConnstring;
            command.Connection.Open();


            base.ScalarExecuting(command, interceptionContext);
        }
    }
Sain的主页 Sain | 初学一级 | 园豆:102
提问于:2016-05-09 11:59
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册