This commit is contained in:
sunkaixuan 2022-10-21 11:56:19 +08:00
parent 9a113c6292
commit 3dbf85df7b

View File

@ -18,6 +18,18 @@ namespace OrmTest
order.GetList(); order.GetList();
order.GetById(1); order.GetById(1);
order.MyTest(); order.MyTest();
var db = new SqlSugarClient(new ConnectionConfig()
{
DbType = SqlSugar.DbType.SqlServer,
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true,
ConnectionString = Config.ConnectionString
});
var repository= db.GetRepository<Repository<Order>>();
var list=repository.GetList();
var repository2 = db.GetRepository<SimpleClient<Order>>();
var list2 = repository2.GetList();
Console.WriteLine("#### SimpleClient End ####"); Console.WriteLine("#### SimpleClient End ####");
} }
public class OrderDal:Repository<Order> public class OrderDal:Repository<Order>
@ -30,25 +42,22 @@ namespace OrmTest
} }
public class Repository<T> : SimpleClient<T> where T : class, new() public class Repository<T> : SimpleClient<T> where T : class, new()
{ {
public Repository(ISqlSugarClient context = null) : base(context)//注意这里要有默认值等于null public Repository()
{ {
if (context == null) var db = new SqlSugarClient(new ConnectionConfig()
{ {
var db = new SqlSugarClient(new ConnectionConfig() DbType = SqlSugar.DbType.SqlServer,
{ InitKeyType = InitKeyType.Attribute,
DbType = SqlSugar.DbType.SqlServer, IsAutoCloseConnection = true,
InitKeyType = InitKeyType.Attribute, ConnectionString = Config.ConnectionString
IsAutoCloseConnection = true, });
ConnectionString = Config.ConnectionString base.Context = db;
}); db.Aop.OnLogExecuting = (s, p) =>
base.Context = db; {
db.Aop.OnLogExecuting = (s, p) => Console.WriteLine(s);
{ };
Console.WriteLine(s);
};
}
} }
/// <summary> /// <summary>
/// 扩展方法,自带方法不能满足的时候可以添加新方法 /// 扩展方法,自带方法不能满足的时候可以添加新方法
/// </summary> /// </summary>