sainSun 2016-05-04 06:15
浏览 1441

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

我想实现数据库的读写分离 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);
        }
    }
  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 保护模式-系统加载-段寄存器