mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-11-08 18:34:55 +08:00
Update demo
This commit is contained in:
@@ -14,58 +14,64 @@ namespace OrmTest
|
|||||||
Console.WriteLine("");
|
Console.WriteLine("");
|
||||||
Console.WriteLine("#### DemoM_UnitOfWork ####");
|
Console.WriteLine("#### DemoM_UnitOfWork ####");
|
||||||
|
|
||||||
|
var db = NewUnitTest.Db;
|
||||||
DbContext.Db.UseTran(() =>
|
db.CurrentConnectionConfig.ConfigId = "1";
|
||||||
|
using (var uow = db.CreateContext<MyDbContext>())
|
||||||
{
|
{
|
||||||
|
var o = uow.GetRepository<ORDER>();
|
||||||
var id = DbContext.CustomDal.InsertReturnIdentity(new Custom() { Id = 1, Name = "guid" });
|
var o2 = uow.GetMyRepository<DbSet<ORDER>>();
|
||||||
var id2 = DbContext.OrderDal.InsertReturnIdentity(new Order() { Name = "guid2", Price = 0, CreateTime = DateTime.Now, CustomId = 1 });
|
var list = o.GetList();//默认仓储
|
||||||
throw new Exception("");
|
var list2 = o2.CommQuery();//自定义仓储
|
||||||
},
|
var list3 = uow.Orders1.GetList();//MyDbContext中的默认仓储
|
||||||
e =>
|
var list4 = uow.Orders2.GetList();//MyDbContext中的自定义仓储
|
||||||
{
|
uow.Commit();
|
||||||
//throw e;
|
|
||||||
});
|
|
||||||
Console.WriteLine("");
|
|
||||||
Console.WriteLine("#### Saveable End ####");
|
|
||||||
}
|
}
|
||||||
public class DbContext
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// 自定义DbContext
|
||||||
|
/// </summary>
|
||||||
|
public class MyDbContext : SugarUnitOfWork
|
||||||
{
|
{
|
||||||
public static SqlSugarScope Db = new SqlSugarScope(new ConnectionConfig()
|
/// <summary>
|
||||||
{
|
/// 原生仓储
|
||||||
DbType = SqlSugar.DbType.SqlServer,
|
/// </summary>
|
||||||
ConnectionString = Config.ConnectionString,
|
public SimpleClient<ORDER> Orders1 { get; set; }
|
||||||
IsAutoCloseConnection = true
|
/// <summary>
|
||||||
}, db => {
|
///自定义仓储
|
||||||
//单例参数配置,所有上下文生效
|
/// </summary>
|
||||||
db.Aop.OnLogExecuting = (s, p) =>
|
public DbSet<ORDER> Orders2 { get; set; }
|
||||||
{
|
}
|
||||||
Console.WriteLine(s);
|
/// <summary>
|
||||||
};
|
/// 自定义仓储
|
||||||
});
|
/// </summary>
|
||||||
|
/// <typeparam name="T"></typeparam>
|
||||||
public static DbSet<Order> OrderDal => new DbSet<Order>();
|
|
||||||
public static DbSet<Custom> CustomDal => new DbSet<Custom>();
|
|
||||||
|
|
||||||
|
|
||||||
public class DbSet<T> : SimpleClient<T> where T : class, new()
|
public class DbSet<T> : SimpleClient<T> where T : class, new()
|
||||||
{
|
{
|
||||||
public DbSet(ISqlSugarClient context = null) : base(context)//需要有构造参数
|
|
||||||
{
|
|
||||||
base.Context = DbContext.Db;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 扩展方法,自带方法不能满足的时候可以添加新方法
|
/// 仓储自定义方法
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public List<T> CommQuery(string json)
|
public List<T> CommQuery()
|
||||||
{
|
{
|
||||||
//base.Context.Queryable<T>().ToList();可以拿到SqlSugarClient 做复杂操作
|
return base.Context.Queryable<T>().ToList();
|
||||||
return null;
|
}
|
||||||
}
|
|
||||||
|
}
|
||||||
}
|
|
||||||
|
[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<OrderItem> Items { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user