It is not the encapsulation of ORM,a based on EF + dapper + Autofac, is repository and unitofworkgit
程序包管理器控制檯,使用 NuGet命令安裝。 PM> Install-Package yrjw.ORM.Chimpgithub
或者直接在項目文件.csproj中引入包api
/// <summary> /// 學生信息表 /// </summary> [Table("StudentInfo")] public class StudentInfo: IEntity { [Key] public virtual int Id { get; set; } /// <summary> /// 學生姓名 /// </summary> [Required] [Column(TypeName = "varchar(50)")] public string Name { get; set; } /// <summary> /// 性別 /// </summary> public int Sex { get; set; } /// <summary> /// 民族 /// </summary> public int NationId { get; set; } /// <summary> /// 電話 /// </summary> public string Phone { get; set; } }
public class myDbContext: BaseDbContext { public myDbContext() { } public myDbContext(DbContextOptions options) : base(options) { } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Seed(); //種子數據 } }
public static class ModelBuilderExtensions { /// <summary> /// 種子數據 /// </summary> /// <param name="modelBuilder"></param> public static void Seed(this ModelBuilder modelBuilder) { modelBuilder.Entity<StudentInfo>().HasData( new StudentInfo { Id = 1, Name = "張三", Sex = 1, NationId = 1, Phone="13902451189"} ); } }
public interface IStudentInfoService { IUnitOfWork UnitOfWork { get; } Task<IResultModel> QueryList(); }
public class StudentInfoService: IStudentInfoService, IDependency { private readonly Lazy<IMapper> _mapper; private readonly Lazy<IRepository<StudentInfo>> repStudentInfo; public IUnitOfWork UnitOfWork { get; } public StudentInfoService(Lazy<IMapper> mapper, IUnitOfWork unitOfWork, Lazy<IRepository<StudentInfo>> repStudentInfo) { this._mapper = mapper; this.UnitOfWork = unitOfWork; this.repStudentInfo = repStudentInfo; } public async Task<IResultModel> QueryList() { var list = await repStudentInfo.Value.TableNoTracking.ProjectTo<StudentInfoDTO>(_mapper.Value.ConfigurationProvider).ToListAsync(); return ResultModel.Success<IList<StudentInfoDTO>>(list); } }
[Description("學生信息")] [Route("api/[controller]/[action]")] public class StudentInfoController : ControllerBase { private readonly ILogger<StudentInfoController> _logger; public Lazy<IStudentInfoService> StudentInfoService { get; set; } public StudentInfoController(ILogger<StudentInfoController> logger) { _logger = logger; } [Description("獲取學生列表")] [ResponseCache(Duration = 0)] [HttpGet] public Task<IResultModel> QueryList() { return StudentInfoService.Value.QueryList(); } }
public virtual void ConfigureServices(IServiceCollection services) { if (setting.DbType == yrjw.ORM.Chimp.DbType.MYSQL) { services.AddChimp<myDbContext>(opt => opt.UseMySql(setting.ConnectionString, b => b.MigrationsAssembly(setting.AssemblyName))); } else { services.AddChimp<myDbContext>(opt => opt.UseSqlServer(setting.ConnectionString, b => b.MigrationsAssembly(setting.AssemblyName))); } }
使用說明app
在Leo.Chimp包基礎上添加了Autofac依賴注入,封裝返回接口模型IResultModel,支持.net core 3.1版本上使用。async