mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-04-30 04:45:54 +08:00
Saveable master-slave BUG
This commit is contained in:
parent
d27dc28df6
commit
5b1d9268d2
@ -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";
|
||||
}
|
||||
}
|
||||
|
@ -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) =>
|
||||
|
@ -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)
|
||||
|
@ -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()
|
||||
|
@ -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; }
|
||||
|
Loading…
Reference in New Issue
Block a user