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