SqlSugar源码
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.

81 lines
3.1 KiB

4 years ago
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
4 years ago
namespace OrmTest
4 years ago
{
public class DemoH_Tenant
{
public static void Init()
{
Console.WriteLine("");
3 years ago
Console.WriteLine("#### DemoH_Tenant Start ####");
4 years ago
new C1Service().Test();
3 years ago
Console.WriteLine("#### DemoH_Tenant End ####");
4 years ago
}
4 years ago
public class C1Service : Repository<C1Table>
{
4 years ago
public void Test()
4 years ago
{
3 years ago
db.BeginTran();
4 years ago
base.GetList(); //调用内部仓储方法
base.ChangeRepository<Repository<C2Table>>().GetList();//调用外部仓储
3 years ago
db.CommitTran();
4 years ago
}
4 years ago
}
4 years ago
public class Repository<T> : SimpleClient<T> where T : class, new()
{
3 years ago
//单例实同db同上下文共享
public static SqlSugarScope db = new SqlSugarScope(new List<ConnectionConfig> {
4 years ago
new ConnectionConfig()
{
4 years ago
ConfigId="1",
4 years ago
DbType = SqlSugar.DbType.SqlServer,
IsAutoCloseConnection = true,
ConnectionString = Config.ConnectionString
},
new ConnectionConfig()
{
ConfigId="2",
DbType = SqlSugar.DbType.SqlServer,
IsAutoCloseConnection = true,
ConnectionString = Config.ConnectionString2
}
});
3 years ago
public Repository(ISqlSugarClient context = null) : base(context)//注意这里要有默认值等于null
{
if (context == null)
{
3 years ago
Context = db.GetConnectionWithAttr<T>();
3 years ago
Context.CodeFirst.InitTables<T>();
4 years ago
}
}
/// <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")]
4 years ago
public class C1Table
4 years ago
{
4 years ago
public string Id { get; set; }
4 years ago
}
[TenantAttribute("2")]
public class C2Table
{
public string Id { get; set; }
}
}
}