You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.4 KiB

2 years ago
  1. using OrmTest.Demo;
  2. using OrmTest.Models;
  3. using SqlSugar;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. namespace OrmTest.Demo
  9. {
  10. public class MasterSlave : DemoBase
  11. {
  12. public static void Init()
  13. {
  14. for (int i = 0; i < 10; i++)
  15. {
  16. var db = GetMasterSlaveInstance();
  17. var list = db.Insertable(new Student() { Name="aa" }).ExecuteCommand(); // ConnectionString2 or ConnectionString3
  18. }
  19. //db.Insertable(new Student() { Name = "masterTest" }).ExecuteCommand();// Config.ConnectionString
  20. }
  21. public static SqlSugarClient GetMasterSlaveInstance()
  22. {
  23. SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
  24. {
  25. ConnectionString = Config.ConnectionString,
  26. DbType = DbType.SqlServer,
  27. IsAutoCloseConnection = true,
  28. SlaveConnectionConfigs = new List<SlaveConnectionConfig>() {
  29. new SlaveConnectionConfig() { HitRate=10, ConnectionString=Config.ConnectionString2 } ,
  30. new SlaveConnectionConfig() { HitRate=10, ConnectionString=Config.ConnectionString2 }
  31. }
  32. });
  33. db.Aop.OnLogExecuting = (sql, pars) =>
  34. {
  35. Console.WriteLine(db.Ado.Connection.ConnectionString);
  36. };
  37. return db;
  38. }
  39. }
  40. }