SqlSugar/Src/Asp.Net/SqlServerTest/OldTest/Demos/A_MasterSlave.cs

50 lines
1.7 KiB
C#
Raw Normal View History

2017-10-24 16:19:14 +08:00
using OrmTest.Demo;
using OrmTest.Models;
using SqlSugar;
using System;
2017-10-24 14:44:33 +08:00
using System.Collections.Generic;
using System.Linq;
using System.Text;
2017-10-24 16:19:14 +08:00
namespace OrmTest.Demo
2017-10-24 14:44:33 +08:00
{
2017-10-24 16:19:14 +08:00
public class MasterSlave : DemoBase
2017-10-24 14:44:33 +08:00
{
2017-10-24 16:19:14 +08:00
public static void Init()
{
2019-03-28 21:07:38 +08:00
Console.WriteLine("");
2017-12-05 16:00:01 +08:00
for (int i = 0; i < 10; i++)
2017-10-24 16:19:14 +08:00
{
2017-10-28 11:11:30 +08:00
var db = GetMasterSlaveInstance();
2019-03-28 21:07:38 +08:00
2017-10-28 11:11:30 +08:00
var list = db.Insertable(new Student() { Name="aa" }).ExecuteCommand(); // ConnectionString2 or ConnectionString3
2019-03-28 21:07:38 +08:00
db.Queryable<Student>().First();
2017-10-24 16:19:14 +08:00
}
2017-10-28 11:11:30 +08:00
//db.Insertable(new Student() { Name = "masterTest" }).ExecuteCommand();// Config.ConnectionString
2019-03-28 21:11:27 +08:00
Console.WriteLine("saveable");
GetMasterSlaveInstance().Saveable(new Student() { Name = "a" }).ExecuteCommand();
2017-10-24 16:19:14 +08:00
}
public static SqlSugarClient GetMasterSlaveInstance()
{
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = Config.ConnectionString,
DbType = DbType.SqlServer,
IsAutoCloseConnection = true,
2017-10-24 17:00:21 +08:00
SlaveConnectionConfigs = new List<SlaveConnectionConfig>() {
2017-10-28 11:11:30 +08:00
new SlaveConnectionConfig() { HitRate=10, ConnectionString=Config.ConnectionString2 } ,
2019-03-28 21:07:38 +08:00
new SlaveConnectionConfig() { HitRate=10, ConnectionString=Config.ConnectionString3 }
2017-10-24 16:19:14 +08:00
}
});
db.Aop.OnLogExecuting = (sql, pars) =>
{
Console.WriteLine(db.Ado.Connection.ConnectionString);
};
return db;
}
2017-10-24 14:44:33 +08:00
}
}