123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- using Microsoft.EntityFrameworkCore;
- using System.ComponentModel.DataAnnotations;
- using System.Linq.Expressions;
- namespace ERP.Framework.WebApi
- {
- public class BaseRepository<T, TDbContext> where T : BaseEntity where TDbContext : DbContext
- {
- public TDbContext Context;
- public BaseRepository(TDbContext context)
- {
- Context = context;
- }
- public T? FirstOrDefault(Expression<Func<T, bool>>? exp = null)
- {
- return Filter(exp).FirstOrDefault();
- }
- public Task<T?> FirstOrDefaultAsync(Expression<Func<T, bool>>? exp = null)
- {
- return Filter(exp).FirstOrDefaultAsync();
- }
- public void Add(T entity)
- {
- Context.Set<T>().Add(entity);
- Save();
- // EF不再跟踪实体更改
- Context.Entry(entity).State = EntityState.Detached;
- }
- public async Task<bool> AddAsync(T entity)
- {
- await Context.Set<T>().AddAsync(entity);
- return await TaskSave();
- }
- public void Save()
- {
- try
- {
- var entities = Context.ChangeTracker.Entries()
- .Where(e => e.State == EntityState.Added
- || e.State == EntityState.Modified)
- .Select(e => e.Entity);
- foreach (var entity in entities)
- {
- var validationContext = new ValidationContext(entity);
- Validator.ValidateObject(entity, validationContext, validateAllProperties: true);
- }
- Context.SaveChanges();
- }
- catch (ValidationException exc)
- {
- // Todo 抛出异常
- //Console.WriteLine($"{nameof(Save)} validation exception: {exc?.Message}");
- //throw (exc.InnerException as Exception ?? exc);
- }
- catch (Exception ex) //DbUpdateException
- {
- // Todo 抛出异常
- //throw (ex.InnerException as Exception ?? ex);
- }
- }
- public Task<bool> TaskSave()
- {
- try
- {
- var entities = Context.ChangeTracker.Entries()
- .Where(e => e.State == EntityState.Added
- || e.State == EntityState.Modified)
- .Select(e => e.Entity);
- foreach (var entity in entities)
- {
- var validationContext = new ValidationContext(entity);
- Validator.ValidateObject(entity, validationContext, validateAllProperties: true);
- }
- var result = Context.SaveChanges();
- return Task.FromResult(result > 0);
- }
- catch (Exception ex)
- {
- // Todo 打印日志
- return Task.FromResult(false); ;
- }
- }
- private IQueryable<T> Filter(Expression<Func<T, bool>>? exp)
- {
- var dbSet = Context.Set<T>().AsNoTracking().AsQueryable();
- if (exp != null)
- dbSet = dbSet.Where(exp);
- return dbSet;
- }
- }
- }
|