SqlSugar/Src/Asp.Net/SqlServerTest/Demo/DemoG_SimpleClient.cs

65 lines
2.0 KiB
C#
Raw Normal View History

2019-06-02 16:38:16 +08:00
using SqlSugar;
using System;
2019-05-05 12:44:43 +08:00
using System.Collections.Generic;
using System.Linq;
using System.Text;
2019-06-02 15:31:27 +08:00
using System.Threading.Tasks;
2019-05-05 12:44:43 +08:00
2019-06-02 15:31:27 +08:00
namespace OrmTest
2019-05-05 12:44:43 +08:00
{
2019-06-02 15:31:27 +08:00
public class DemoG_SimpleClient
2019-05-05 12:44:43 +08:00
{
2019-06-02 15:31:27 +08:00
public static void Init()
{
2019-06-02 16:38:16 +08:00
Console.WriteLine("");
Console.WriteLine("#### SimpleClient Start ####");
2019-06-02 15:31:27 +08:00
2021-04-06 16:41:08 +08:00
var order = new OrderDal();
order.GetList();
order.GetById(1);
order.MyTest();
Console.WriteLine("#### SimpleClient End ####");
}
public class OrderDal:Repository<Order>
{
2021-04-06 21:21:46 +08:00
public void MyTest()
{
base.CommQuery("1=1");
base.ChangeRepository<Repository<OrderItem>>().CommQuery("1=1");
2021-04-06 16:41:08 +08:00
}
}
public class Repository<T> : SimpleClient<T> where T : class, new()
{
public Repository(ISqlSugarClient context = null) : base(context)//注意这里要有默认值等于null
2019-06-02 16:38:16 +08:00
{
2021-04-06 16:41:08 +08:00
if (context == null)
2019-06-02 16:38:16 +08:00
{
2021-04-06 21:21:46 +08:00
var db = new SqlSugarClient(new ConnectionConfig()
2019-06-02 16:38:16 +08:00
{
2021-04-06 16:41:08 +08:00
DbType = SqlSugar.DbType.SqlServer,
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true,
ConnectionString = Config.ConnectionString
});
2021-04-06 21:21:46 +08:00
base.Context = db;
db.Aop.OnLogExecuting = (s, p) =>
{
Console.WriteLine(s);
};
2019-06-02 16:38:16 +08:00
}
2021-04-06 16:41:08 +08:00
}
/// <summary>
/// 扩展方法,自带方法不能满足的时候可以添加新方法
/// </summary>
/// <returns></returns>
2021-04-06 21:21:46 +08:00
public List<T> CommQuery(string sql)
2021-04-06 16:41:08 +08:00
{
//base.Context.Queryable<T>().ToList();可以拿到SqlSugarClient 做复杂操作
2021-04-06 21:21:46 +08:00
return base.Context.Queryable<T>().Where(sql).ToList();
2021-04-06 16:41:08 +08:00
}
2019-06-02 16:38:16 +08:00
2019-06-02 15:31:27 +08:00
}
2019-05-05 12:44:43 +08:00
}
}