Browse Source

Update demo

pull/6/head
sunkaixuna 3 years ago
parent
commit
a49abdcd73
  1. 23
      Src/Asp.Net/SqlServerTest/Demo/DemoH_Tenant.cs

23
Src/Asp.Net/SqlServerTest/Demo/DemoH_Tenant.cs

@ -21,23 +21,16 @@ namespace OrmTest
{ {
public void Test() public void Test()
{ {
base.db.BeginTran(); db.BeginTran();
base.GetList(); //调用内部仓储方法 base.GetList(); //调用内部仓储方法
base.ChangeRepository<Repository<C2Table>>().GetList();//调用外部仓储 base.ChangeRepository<Repository<C2Table>>().GetList();//调用外部仓储
db.CommitTran();
base.db.CommitTran();
} }
} }
public class Repository<T> : SimpleClient<T> where T : class, new() public class Repository<T> : SimpleClient<T> where T : class, new()
{ {
public SqlSugarClient db; //单例实同db同上下文共享
public Repository(ISqlSugarClient context = null) : base(context)//注意这里要有默认值等于null public static SqlSugarScope db = new SqlSugarScope(new List<ConnectionConfig> {
{
if (context == null)
{
var db = new SqlSugarClient(new List<ConnectionConfig> {
new ConnectionConfig() new ConnectionConfig()
{ {
ConfigId="1", ConfigId="1",
@ -53,11 +46,13 @@ namespace OrmTest
ConnectionString = Config.ConnectionString2 ConnectionString = Config.ConnectionString2
} }
}); });
public Repository(ISqlSugarClient context = null) : base(context)//注意这里要有默认值等于null
{
if (context == null)
{
var configId = typeof(T).GetCustomAttribute<TenantAttribute>().configId; var configId = typeof(T).GetCustomAttribute<TenantAttribute>().configId;
Context = db.GetConnection(configId); Context = db.GetConnection(configId);
this.db = db; Context.CodeFirst.InitTables<T>();
this.db.CodeFirst.InitTables<T>();
} }
} }

Loading…
Cancel
Save