2 sainsun sainSun 于 2016.05.04 14:15 提问

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);
        }
    }

1个回答

CSDNXIAOD
CSDNXIAOD   2016.05.04 14:22

Qt数据库连接、数据查询、添加、修改、删除
----------------------biu~biu~biu~~~在下问答机器人小D,这是我依靠自己的聪明才智给出的答案,如果不正确,你来咬我啊!

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!