Saveable master-slave BUG

This commit is contained in:
sunkaixuan 2019-03-28 21:07:38 +08:00
parent d27dc28df6
commit 5b1d9268d2
5 changed files with 17 additions and 6 deletions

View File

@ -8,8 +8,8 @@ namespace OrmTest
{
public class Config
{
public static string ConnectionString = "server=.;uid=sa;pwd=@jhl85661501;database=SqlSugar4XTest";
public static string ConnectionString2 = "server=.;uid=sa;pwd=haosql;database=SQLSUGAR4XTEST";
public static string ConnectionString3 = "server=.;uid=sa;pwd=haosql;database=sqlsugar4xtest";
public static string ConnectionString = "server=.;uid=sa;pwd=@jhl85661501;database=SQLSUGAR4XTEST";
public static string ConnectionString2 = "server=.;uid=sa;pwd=@jhl85661501;database=sqlsugar4xtest";
public static string ConnectionString3 = "server=.;uid=sa;pwd=@jhl85661501;database=sqlsugar4xtesT";
}
}

View File

@ -13,11 +13,14 @@ namespace OrmTest.Demo
public static void Init()
{
Console.WriteLine("");
for (int i = 0; i < 10; i++)
{
var db = GetMasterSlaveInstance();
var list = db.Insertable(new Student() { Name="aa" }).ExecuteCommand(); // ConnectionString2 or ConnectionString3
db.Queryable<Student>().First();
}
//db.Insertable(new Student() { Name = "masterTest" }).ExecuteCommand();// Config.ConnectionString
}
@ -31,7 +34,7 @@ namespace OrmTest.Demo
IsAutoCloseConnection = true,
SlaveConnectionConfigs = new List<SlaveConnectionConfig>() {
new SlaveConnectionConfig() { HitRate=10, ConnectionString=Config.ConnectionString2 } ,
new SlaveConnectionConfig() { HitRate=10, ConnectionString=Config.ConnectionString2 }
new SlaveConnectionConfig() { HitRate=10, ConnectionString=Config.ConnectionString3 }
}
});
db.Aop.OnLogExecuting = (sql, pars) =>

View File

@ -39,6 +39,7 @@ namespace SqlSugar
internal bool OldClearParameters { get; set; }
public IDataParameterCollection DataReaderParameters { get; set; }
public TimeSpan SqlExecutionTime { get { return AfterTime - BeforeTime; } }
public bool IsDisableMasterSlaveSeparation { get; set; }
internal DateTime BeforeTime = DateTime.MinValue;
internal DateTime AfterTime = DateTime.MinValue;
public virtual IDbBind DbBind
@ -761,7 +762,7 @@ namespace SqlSugar
{
get
{
return this.Context.CurrentConnectionConfig.SlaveConnectionConfigs.HasValue();
return this.Context.CurrentConnectionConfig.SlaveConnectionConfigs.HasValue()&& this.IsDisableMasterSlaveSeparation==false;
}
}
private void SetConnectionStart(string sql)

View File

@ -26,6 +26,8 @@ namespace SqlSugar
{
get
{
var isDisableMasterSlaveSeparation = this.Context.Ado.IsDisableMasterSlaveSeparation;
this.Context.Ado.IsDisableMasterSlaveSeparation = true;
List<T> result = new List<T>();
var pks = GetPrimaryKeys();
Check.Exception(pks.IsNullOrEmpty(), "Need primary key");
@ -34,6 +36,7 @@ namespace SqlSugar
var pkValues = saveObjects.Select(it=>it.GetType().GetProperty(pkInfo.PropertyName).GetValue(it,null));
if(existsObjects==null)
existsObjects=this.Context.Queryable<T>().In(pkValues).ToList();
this.Context.Ado.IsDisableMasterSlaveSeparation = isDisableMasterSlaveSeparation;
return saveObjects.Where(it=>!
existsObjects.Any(e=>
e.GetType().GetProperty(pkInfo.PropertyName).GetValue(e,null).ObjToString()
@ -45,6 +48,8 @@ namespace SqlSugar
{
get
{
var isDisableMasterSlaveSeparation = this.Context.Ado.IsDisableMasterSlaveSeparation;
this.Context.Ado.IsDisableMasterSlaveSeparation = true;
List<T> result = new List<T>();
var pks = GetPrimaryKeys();
Check.Exception(pks.IsNullOrEmpty(), "Need primary key");
@ -53,6 +58,7 @@ namespace SqlSugar
var pkValues = saveObjects.Select(it => it.GetType().GetProperty(pkInfo.PropertyName).GetValue(it, null));
if (existsObjects == null)
existsObjects = this.Context.Queryable<T>().In(pkValues).ToList();
this.Context.Ado.IsDisableMasterSlaveSeparation = isDisableMasterSlaveSeparation;
return saveObjects.Where(it =>
existsObjects.Any(e =>
e.GetType().GetProperty(pkInfo.PropertyName).GetValue(e, null).ObjToString()

View File

@ -23,6 +23,7 @@ namespace SqlSugar
IDataParameterCollection DataReaderParameters { get; set; }
CommandType CommandType { get; set; }
bool IsEnableLogEvent { get; set; }
bool IsDisableMasterSlaveSeparation { get; set; }
Action<string, SugarParameter []> LogEventStarting { get; set; }
Action<string, SugarParameter []> LogEventCompleted { get; set; }
Func<string, SugarParameter[], KeyValuePair<string, SugarParameter[]>> ProcessingEventStartingSQL { get; set; }