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 使用C#,asp.net读取Excel文件并保存到Oracle数据库
    • ¥15 C# datagridview 单元格显示进度及值
    • ¥15 thinkphp6配合social login单点登录问题
    • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配