diff --git a/Src/Asp.NetCore2/SqlSeverTest/Demo/DemoM_UnitOfWork.cs b/Src/Asp.NetCore2/SqlSeverTest/Demo/DemoM_UnitOfWork.cs index 2c8006cc1..822822892 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/Demo/DemoM_UnitOfWork.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/Demo/DemoM_UnitOfWork.cs @@ -14,59 +14,65 @@ namespace OrmTest Console.WriteLine(""); Console.WriteLine("#### DemoM_UnitOfWork ####"); - - DbContext.Db.UseTran(() => + var db = NewUnitTest.Db; + db.CurrentConnectionConfig.ConfigId = "1"; + using (var uow = db.CreateContext()) { - - var id = DbContext.CustomDal.InsertReturnIdentity(new Custom() { Id = 1, Name = "guid" }); - var id2 = DbContext.OrderDal.InsertReturnIdentity(new Order() { Name = "guid2", Price = 0, CreateTime = DateTime.Now, CustomId = 1 }); - throw new Exception(""); - }, - e => - { - //throw e; - }); - Console.WriteLine(""); - Console.WriteLine("#### Saveable End ####"); - } - public class DbContext - { - public static SqlSugarScope Db = new SqlSugarScope(new ConnectionConfig() - { - DbType = SqlSugar.DbType.SqlServer, - ConnectionString = Config.ConnectionString, - IsAutoCloseConnection = true - }, db => { - //单例参数配置,所有上下文生效 - db.Aop.OnLogExecuting = (s, p) => - { - Console.WriteLine(s); - }; - }); - - public static DbSet OrderDal => new DbSet(); - public static DbSet CustomDal => new DbSet(); - - - public class DbSet : SimpleClient where T : class, new() - { - public DbSet(ISqlSugarClient context = null) : base(context)//需要有构造参数 - { - base.Context = DbContext.Db; - } - - /// - /// 扩展方法,自带方法不能满足的时候可以添加新方法 - /// - /// - public List CommQuery(string json) - { - //base.Context.Queryable().ToList();可以拿到SqlSugarClient 做复杂操作 - return null; - } - + var o = uow.GetRepository(); + var o2 = uow.GetMyRepository>(); + var list = o.GetList();//默认仓储 + var list2 = o2.CommQuery();//自定义仓储 + var list3 = uow.Orders1.GetList();//MyDbContext中的默认仓储 + var list4 = uow.Orders2.GetList();//MyDbContext中的自定义仓储 + uow.Commit(); } } + /// + /// 自定义DbContext + /// + public class MyDbContext : SugarUnitOfWork + { + /// + /// 原生仓储 + /// + public SimpleClient Orders1 { get; set; } + /// + ///自定义仓储 + /// + public DbSet Orders2 { get; set; } + } + /// + /// 自定义仓储 + /// + /// + public class DbSet : SimpleClient where T : class, new() + { + /// + /// 仓储自定义方法 + /// + /// + public List CommQuery() + { + return base.Context.Queryable().ToList(); + } + + } + + [Tenant("1")] + public class ORDER + { + [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] + public int Id { get; set; } + + public string Name { get; set; } + public decimal Price { get; set; } + [SugarColumn(IsNullable = true)] + public DateTime CreateTime { get; set; } + [SugarColumn(IsNullable = true)] + public int CustomId { get; set; } + [SugarColumn(IsIgnore = true)] + public List Items { get; set; } + } } }