diff --git a/Src/Asp.Net/MySqlTest/Config.cs b/Src/Asp.Net/MySqlTest/Config.cs index d017f8af4..b3b355553 100644 --- a/Src/Asp.Net/MySqlTest/Config.cs +++ b/Src/Asp.Net/MySqlTest/Config.cs @@ -9,5 +9,7 @@ namespace OrmTest public class Config { public static string ConnectionString = "server=localhost;Database=SqlSugar4xTest;Uid=root;Pwd=root"; + public static string ConnectionString2 = "server=localhost;Database=SQLSUGAR4XTEST;Uid=root;Pwd=root"; + public static string ConnectionString3 = "server=localhost;Database=sqlsugar4xtest;Uid=root;Pwd=root"; } } diff --git a/Src/Asp.Net/MySqlTest/Demos/A_MasterSlave.cs b/Src/Asp.Net/MySqlTest/Demos/A_MasterSlave.cs new file mode 100644 index 000000000..6aea22c1d --- /dev/null +++ b/Src/Asp.Net/MySqlTest/Demos/A_MasterSlave.cs @@ -0,0 +1,43 @@ +using OrmTest.Demo; +using OrmTest.Models; +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace OrmTest.Demo +{ + public class MasterSlave : DemoBase + { + + public static void Init() + { + var db = GetMasterSlaveInstance(); + for (int i = 0; i < 10; i++) + { + var list = db.Queryable().ToList(); // ConnectionString2 or ConnectionString3 + } + db.Insertable(new Student() { Name = "masterTest" }).ExecuteCommand();// Config.ConnectionString + } + + public static SqlSugarClient GetMasterSlaveInstance() + { + SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() + { + ConnectionString = Config.ConnectionString, + DbType = DbType.MySql, + IsAutoCloseConnection = true, + SlaveConnectionStrings = new List() { + new SlaveConnectionConfig() { HitRate=10, ConnectionString=Config.ConnectionString2 }, + new SlaveConnectionConfig() { HitRate=30, ConnectionString=Config.ConnectionString3 } + } + }); + db.Aop.OnLogExecuting = (sql, pars) => + { + Console.WriteLine(db.Ado.Connection.ConnectionString); + }; + return db; + } + } +} diff --git a/Src/Asp.Net/MySqlTest/MySqlTest.csproj b/Src/Asp.Net/MySqlTest/MySqlTest.csproj index b1084857b..b7379697f 100644 --- a/Src/Asp.Net/MySqlTest/MySqlTest.csproj +++ b/Src/Asp.Net/MySqlTest/MySqlTest.csproj @@ -57,6 +57,7 @@ + diff --git a/Src/Asp.Net/MySqlTest/Program.cs b/Src/Asp.Net/MySqlTest/Program.cs index cea8c21c8..5507e386b 100644 --- a/Src/Asp.Net/MySqlTest/Program.cs +++ b/Src/Asp.Net/MySqlTest/Program.cs @@ -44,6 +44,7 @@ namespace OrmTest OrmTest.Demo.Filter.Init(); OrmTest.Demo.ComplexModel.Init(); OrmTest.Demo.CodeFirst.Init();; + OrmTest.Demo.MasterSlave.Init(); ; } } }