mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-08-23 13:06:50 +08:00
Update demo
This commit is contained in:
parent
4dc5185f04
commit
11f0204af9
81
Src/Asp.Net/SqlServerTest/Demo/DemoH_Tenant.cs
Normal file
81
Src/Asp.Net/SqlServerTest/Demo/DemoH_Tenant.cs
Normal file
@ -0,0 +1,81 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
public class DemoH_Tenant
|
||||
{
|
||||
public static void Init()
|
||||
{
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("#### SimpleClient Start ####");
|
||||
var rep1=new Repository<C1Table>();
|
||||
rep1.GetList();
|
||||
var rep2 = new Repository<C2Table>();
|
||||
rep2.GetList();
|
||||
Console.WriteLine("#### SimpleClient End ####");
|
||||
}
|
||||
|
||||
public class Repository<T> : SimpleClient<T> where T : class, new()
|
||||
{
|
||||
public Repository(ISqlSugarClient context = null) : base(context)//注意这里要有默认值等于null
|
||||
{
|
||||
if (context == null)
|
||||
{
|
||||
var db = new SqlSugarClient(new List<ConnectionConfig> {
|
||||
new ConnectionConfig()
|
||||
{
|
||||
ConfigId=1,
|
||||
DbType = SqlSugar.DbType.SqlServer,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
IsAutoCloseConnection = true,
|
||||
ConnectionString = Config.ConnectionString
|
||||
},
|
||||
new ConnectionConfig()
|
||||
{
|
||||
ConfigId="2",
|
||||
DbType = SqlSugar.DbType.SqlServer,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
IsAutoCloseConnection = true,
|
||||
ConnectionString = Config.ConnectionString2
|
||||
}
|
||||
});
|
||||
var configId = typeof(T).GetCustomAttribute<TenantAttribute>().configId;
|
||||
base.Context = db.GetConnection(configId);
|
||||
base.Context.CodeFirst.InitTables<T>();
|
||||
db.Aop.OnLogExecuting = (s, p) =>
|
||||
{
|
||||
Console.WriteLine(s);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 扩展方法,自带方法不能满足的时候可以添加新方法
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public List<T> CommQuery(string sql)
|
||||
{
|
||||
//base.Context.Queryable<T>().ToList();可以拿到SqlSugarClient 做复杂操作
|
||||
return base.Context.Queryable<T>().Where(sql).ToList();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
[TenantAttribute("1")]
|
||||
public class C1Table
|
||||
{
|
||||
public string Id { get; set; }
|
||||
}
|
||||
[TenantAttribute("2")]
|
||||
public class C2Table
|
||||
{
|
||||
public string Id { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
@ -30,9 +30,9 @@ namespace OrmTest
|
||||
DemoE_CodeFirst.Init();
|
||||
DemoF_Utilities.Init();
|
||||
DemoG_SimpleClient.Init();
|
||||
|
||||
DemoH_Tenant.Init();
|
||||
//Unit test
|
||||
//NewUnitTest.Init();
|
||||
//NewUnitTest.Init();
|
||||
|
||||
//Rest Data
|
||||
NewUnitTest.RestData();
|
||||
|
@ -67,6 +67,7 @@
|
||||
<Compile Include="Demo\DemoE_CodeFirst.cs" />
|
||||
<Compile Include="Demo\DemoF_Utilities.cs" />
|
||||
<Compile Include="Demo\DemoG_SimpleClient.cs" />
|
||||
<Compile Include="Demo\DemoH_Tenant.cs" />
|
||||
<Compile Include="Models\Custom.cs" />
|
||||
<Compile Include="Models\EntityMapper.cs" />
|
||||
<Compile Include="Models\Mapper.cs" />
|
||||
|
@ -192,4 +192,15 @@ namespace SqlSugar
|
||||
|
||||
}
|
||||
|
||||
|
||||
[AttributeUsage(AttributeTargets.Class, Inherited = true)]
|
||||
public class TenantAttribute :Attribute
|
||||
{
|
||||
public object configId { get; set; }
|
||||
public TenantAttribute(object configId)
|
||||
{
|
||||
this.configId = configId;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user