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

79 lines
2.4 KiB
C#
Raw Normal View History

2021-08-28 22:30:33 +08:00
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OrmTest
{
public class DemoM_UnitOfWork
{
public static void Init()
{
Console.WriteLine("");
Console.WriteLine("#### DemoM_UnitOfWork ####");
2022-05-23 23:48:27 +08:00
var db=NewUnitTest.Db;
2022-05-24 01:34:05 +08:00
db.CurrentConnectionConfig.ConfigId = "1";
2022-05-23 23:48:27 +08:00
using (var uow = db.CreateContext<MyDbContext>())
{
2022-05-24 01:34:05 +08:00
var o = uow.GetRepository<ORDER>();
var o2 = uow.GetMyRepository<DbSet<ORDER>>();
2022-05-23 23:48:27 +08:00
var list = o.GetList();//默认仓储
var list2 = o2.CommQuery();//自定义仓储
var list3 = uow.Orders1.GetList();//MyDbContext中的默认仓储
var list4 = uow.Orders2.GetList();//MyDbContext中的自定义仓储
uow.Commit();
}
2021-08-28 22:30:33 +08:00
}
2022-05-23 23:48:27 +08:00
/// <summary>
/// 自定义DbContext
/// </summary>
public class MyDbContext : SugarUnitOfWork
2021-08-28 22:30:33 +08:00
{
2022-05-23 23:48:27 +08:00
/// <summary>
/// 原生仓储
/// </summary>
2022-05-24 01:34:05 +08:00
public SimpleClient<ORDER> Orders1 { get; set; }
2022-05-23 23:48:27 +08:00
/// <summary>
///自定义仓储
/// </summary>
2022-05-24 01:34:05 +08:00
public DbSet<ORDER> Orders2 { get; set; }
2022-05-23 23:48:27 +08:00
}
/// <summary>
/// 自定义仓储
/// </summary>
/// <typeparam name="T"></typeparam>
public class DbSet<T> : SimpleClient<T> where T : class, new()
{
/// <summary>
/// 仓储自定义方法
/// </summary>
/// <returns></returns>
public List<T> CommQuery()
2021-08-28 22:30:33 +08:00
{
2022-05-23 23:48:27 +08:00
return base.Context.Queryable<T>().ToList();
2021-08-28 22:30:33 +08:00
}
2022-05-23 23:48:27 +08:00
2021-08-28 22:30:33 +08:00
}
2022-05-24 01:34:05 +08:00
[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; }
}
2021-08-28 22:30:33 +08:00
}
}