mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-08-23 22:11:36 +08:00
Update demo
This commit is contained in:
parent
8ff5ac17fb
commit
e9e497db75
@ -21,18 +21,19 @@ namespace OrmTest
|
||||
{
|
||||
public void Test()
|
||||
{
|
||||
base.AsTenant().BeginTran();
|
||||
base.db.BeginTran();
|
||||
|
||||
base.GetList(); //调用内部仓储方法
|
||||
base.ChangeRepository<Repository<C2Table>>().GetList();//调用外部仓储
|
||||
|
||||
base.AsTenant().CommitTran();
|
||||
base.db.CommitTran();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class Repository<T> : SimpleClient<T> where T : class, new()
|
||||
{
|
||||
public SqlSugarClient db;
|
||||
public Repository(ISqlSugarClient context = null) : base(context)//注意这里要有默认值等于null
|
||||
{
|
||||
if (context == null)
|
||||
@ -53,10 +54,10 @@ namespace OrmTest
|
||||
ConnectionString = Config.ConnectionString2
|
||||
}
|
||||
});
|
||||
base.Context = db;
|
||||
var configId = typeof(T).GetCustomAttribute<TenantAttribute>().configId;
|
||||
db.ChangeDatabase(configId);
|
||||
|
||||
var configId = typeof(T).GetCustomAttribute<TenantAttribute>().configId;
|
||||
Context = db.GetConnection(configId);
|
||||
this.db = db;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,87 @@
|
||||
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 ####");
|
||||
new C1Service().Test();
|
||||
Console.WriteLine("#### SimpleClient End ####");
|
||||
}
|
||||
public class C1Service : Repository<C1Table>
|
||||
{
|
||||
public void Test()
|
||||
{
|
||||
base.db.BeginTran();
|
||||
|
||||
base.GetList(); //调用内部仓储方法
|
||||
base.ChangeRepository<Repository<C2Table>>().GetList();//调用外部仓储
|
||||
|
||||
base.db.CommitTran();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class Repository<T> : SimpleClient<T> where T : class, new()
|
||||
{
|
||||
public SqlSugarClient db;
|
||||
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,
|
||||
IsAutoCloseConnection = true,
|
||||
ConnectionString = Config.ConnectionString
|
||||
},
|
||||
new ConnectionConfig()
|
||||
{
|
||||
ConfigId="2",
|
||||
DbType = SqlSugar.DbType.SqlServer,
|
||||
IsAutoCloseConnection = true,
|
||||
ConnectionString = Config.ConnectionString2
|
||||
}
|
||||
});
|
||||
|
||||
var configId = typeof(T).GetCustomAttribute<TenantAttribute>().configId;
|
||||
Context = db.GetConnection(configId);
|
||||
this.db = db;
|
||||
}
|
||||
}
|
||||
|
||||
/// <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; }
|
||||
}
|
||||
}
|
||||
}
|
142
Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demo/DemoJ_Report.cs
Normal file
142
Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demo/DemoJ_Report.cs
Normal file
@ -0,0 +1,142 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
public class DemoJ_Report
|
||||
{
|
||||
public static void Init()
|
||||
{
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("#### Utilities Start ####");
|
||||
|
||||
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
|
||||
{
|
||||
DbType = DbType.SqlServer,
|
||||
ConnectionString = Config.ConnectionString,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
IsAutoCloseConnection = true,
|
||||
AopEvents = new AopEvents
|
||||
{
|
||||
OnLogExecuting = (sql, p) =>
|
||||
{
|
||||
Console.WriteLine(sql);
|
||||
Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
|
||||
}
|
||||
}
|
||||
});
|
||||
Demo1(db);
|
||||
Demo2(db);
|
||||
Demo3(db);
|
||||
}
|
||||
|
||||
private static void Demo1(SqlSugarClient db)
|
||||
{
|
||||
var list = new List<int>() { 1, 2, 3 };
|
||||
var query1 = db.Queryable<Order>();
|
||||
var queryable2 = db.Reportable(list).ToQueryable<int>();
|
||||
var x = db.Queryable(query1, queryable2, (x1, x2) => x1.Id.Equals(x2.ColumnName))
|
||||
.Select((x1, x2) => new { x = x1.Id, x2 = x2.ColumnName }).ToList();
|
||||
}
|
||||
private static void Demo2(SqlSugarClient db)
|
||||
{
|
||||
var list = db.Queryable<OrderItem>().ToList();
|
||||
var query1 = db.Queryable<Order>();
|
||||
var queryable2 = db.Reportable(list).ToQueryable();
|
||||
var x = db.Queryable(query1, queryable2, (x1, x2) => x1.Id.Equals(x2.OrderId))
|
||||
.Select((x1, x2) => new { name = x1.Name,id=x1.Id, orderid = x2.OrderId }).ToList();
|
||||
}
|
||||
private static void Demo3(SqlSugarClient db)
|
||||
{
|
||||
db.CodeFirst.InitTables<operateinfo>();
|
||||
db.Deleteable<operateinfo>().ExecuteCommand();
|
||||
db.Insertable(new operateinfo()
|
||||
{
|
||||
id=1,
|
||||
operate_type=1,
|
||||
operate_time=Convert.ToDateTime("2021-1-1")
|
||||
}).ExecuteCommand();
|
||||
db.Insertable(new operateinfo()
|
||||
{
|
||||
id = 1,
|
||||
operate_type = 1,
|
||||
operate_time = Convert.ToDateTime("2021-1-2")
|
||||
}).ExecuteCommand();
|
||||
db.Insertable(new operateinfo()
|
||||
{
|
||||
id = 1,
|
||||
operate_type = 1,
|
||||
operate_time = Convert.ToDateTime("2021-3-1")
|
||||
}).ExecuteCommand();
|
||||
db.Insertable(new operateinfo()
|
||||
{
|
||||
id = 1,
|
||||
operate_type = 1,
|
||||
operate_time = Convert.ToDateTime("2021-3-2")
|
||||
}).ExecuteCommand();
|
||||
db.Insertable(new operateinfo()
|
||||
{
|
||||
id = 1,
|
||||
operate_type = 1,
|
||||
operate_time = Convert.ToDateTime("2021-4-2")
|
||||
}).ExecuteCommand();
|
||||
|
||||
|
||||
var queryableLeft = db.Reportable(ReportableDateType.MonthsInLast1years).ToQueryable<DateTime>();
|
||||
var queryableRight = db.Queryable<operateinfo>();
|
||||
var list= db.Queryable(queryableLeft, queryableRight, JoinType.Left,
|
||||
(x1, x2) => x2.operate_time.ToString("yyyy-MM")== x1.ColumnName .ToString("yyyy-MM"))
|
||||
.GroupBy((x1,x2)=>x1.ColumnName)
|
||||
.Where(x1=>SqlFunc.Between(x1.ColumnName,"2021-01-01",DateTime.Now))
|
||||
.Select((x1, x2) => new
|
||||
{
|
||||
count=SqlFunc.AggregateSum(SqlFunc.IIF(x2.id>0,1,0)) ,
|
||||
date=x1.ColumnName.ToString("yyyy-MM")
|
||||
|
||||
}).ToList();
|
||||
}
|
||||
|
||||
|
||||
public partial class operateinfo
|
||||
{
|
||||
public operateinfo()
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// Desc:操作序号
|
||||
/// Default:
|
||||
/// Nullable:False
|
||||
/// </summary>
|
||||
public int id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Desc:操作时间
|
||||
/// Default:
|
||||
/// Nullable:False
|
||||
/// </summary>
|
||||
public DateTime operate_time { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Desc:操作类型
|
||||
/// Default:
|
||||
/// Nullable:False
|
||||
/// </summary>
|
||||
public int operate_type { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Desc:操作人编号
|
||||
/// Default:
|
||||
/// Nullable:False
|
||||
/// </summary>
|
||||
public int user_id { get; set; }
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -31,7 +31,7 @@ namespace OrmTest
|
||||
DemoE_CodeFirst.Init();
|
||||
DemoF_Utilities.Init();
|
||||
DemoG_SimpleClient.Init();
|
||||
|
||||
DemoJ_Report.Init();
|
||||
//Unit test
|
||||
//NewUnitTest.Init();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user