2018-05-07 14:47:16 +08:00
|
|
|
|
using System;
|
2020-10-22 14:59:36 +08:00
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
2018-05-07 14:47:16 +08:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Linq.Expressions;
|
|
|
|
|
using Infrastructure;
|
2020-10-22 14:59:36 +08:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using OpenAuth.Repository.Core;
|
2018-05-07 14:47:16 +08:00
|
|
|
|
using OpenAuth.Repository.Interface;
|
2020-10-22 14:59:36 +08:00
|
|
|
|
using Z.EntityFramework.Plus;
|
2018-05-07 14:47:16 +08:00
|
|
|
|
|
|
|
|
|
namespace OpenAuth.Repository
|
|
|
|
|
{
|
|
|
|
|
public class UnitWork: IUnitWork
|
|
|
|
|
{
|
2020-10-22 14:59:36 +08:00
|
|
|
|
private OpenAuthDBContext _context;
|
2018-05-07 14:47:16 +08:00
|
|
|
|
|
2020-10-22 14:59:36 +08:00
|
|
|
|
public UnitWork(OpenAuthDBContext context)
|
|
|
|
|
{
|
|
|
|
|
_context = context;
|
|
|
|
|
}
|
|
|
|
|
public OpenAuthDBContext GetDbContext()
|
|
|
|
|
{
|
|
|
|
|
return _context;
|
|
|
|
|
}
|
2018-05-07 14:47:16 +08:00
|
|
|
|
|
2020-10-22 14:59:36 +08:00
|
|
|
|
/// <summary>
|
2018-05-07 14:47:16 +08:00
|
|
|
|
/// 根据过滤条件,获取记录
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="exp">The exp.</param>
|
|
|
|
|
public IQueryable<T> Find<T>(Expression<Func<T, bool>> exp = null) where T : class
|
|
|
|
|
{
|
|
|
|
|
return Filter(exp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsExist<T>(Expression<Func<T, bool>> exp) where T : class
|
|
|
|
|
{
|
2020-10-22 14:59:36 +08:00
|
|
|
|
return _context.Set<T>().Any(exp);
|
2018-05-07 14:47:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 查找单个
|
|
|
|
|
/// </summary>
|
|
|
|
|
public T FindSingle<T>(Expression<Func<T, bool>> exp) where T:class
|
|
|
|
|
{
|
2020-10-22 14:59:36 +08:00
|
|
|
|
return _context.Set<T>().AsNoTracking().FirstOrDefault(exp);
|
2018-05-07 14:47:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 得到分页记录
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="pageindex">The pageindex.</param>
|
|
|
|
|
/// <param name="pagesize">The pagesize.</param>
|
|
|
|
|
/// <param name="orderby">排序,格式如:"Id"/"Id descending"</param>
|
|
|
|
|
public IQueryable<T> Find<T>(int pageindex, int pagesize, string orderby = "", Expression<Func<T, bool>> exp = null) where T : class
|
|
|
|
|
{
|
|
|
|
|
if (pageindex < 1) pageindex = 1;
|
|
|
|
|
if (string.IsNullOrEmpty(orderby))
|
|
|
|
|
orderby = "Id descending";
|
|
|
|
|
|
|
|
|
|
return Filter(exp).OrderBy(orderby).Skip(pagesize * (pageindex - 1)).Take(pagesize);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 根据过滤条件获取记录数
|
|
|
|
|
/// </summary>
|
|
|
|
|
public int GetCount<T>(Expression<Func<T, bool>> exp = null) where T : class
|
|
|
|
|
{
|
|
|
|
|
return Filter(exp).Count();
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-22 14:59:36 +08:00
|
|
|
|
public void Add<T>(T entity) where T : BaseEntity
|
2018-05-07 14:47:16 +08:00
|
|
|
|
{
|
2020-10-22 14:59:36 +08:00
|
|
|
|
if (entity.KeyIsNull())
|
2018-05-07 14:47:16 +08:00
|
|
|
|
{
|
2020-10-22 14:59:36 +08:00
|
|
|
|
entity.GenerateDefaultKeyVal();
|
2018-05-07 14:47:16 +08:00
|
|
|
|
}
|
2020-10-22 14:59:36 +08:00
|
|
|
|
_context.Set<T>().Add(entity);
|
2018-05-07 14:47:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 批量添加
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="entities">The entities.</param>
|
2020-10-22 14:59:36 +08:00
|
|
|
|
public void BatchAdd<T>(T[] entities) where T : BaseEntity
|
2018-05-07 14:47:16 +08:00
|
|
|
|
{
|
|
|
|
|
foreach (var entity in entities)
|
|
|
|
|
{
|
2020-10-22 14:59:36 +08:00
|
|
|
|
if (entity.KeyIsNull())
|
|
|
|
|
{
|
|
|
|
|
entity.GenerateDefaultKeyVal();
|
|
|
|
|
}
|
2018-05-07 14:47:16 +08:00
|
|
|
|
}
|
2020-10-22 14:59:36 +08:00
|
|
|
|
_context.Set<T>().AddRange(entities);
|
2018-05-07 14:47:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Update<T>(T entity) where T:class
|
|
|
|
|
{
|
2020-10-22 14:59:36 +08:00
|
|
|
|
var entry = this._context.Entry(entity);
|
2018-05-07 14:47:16 +08:00
|
|
|
|
entry.State = EntityState.Modified;
|
|
|
|
|
|
2018-05-20 13:54:56 +08:00
|
|
|
|
//如果数据没有发生变化
|
2020-10-22 14:59:36 +08:00
|
|
|
|
if (!this._context.ChangeTracker.HasChanges())
|
2018-05-20 13:54:56 +08:00
|
|
|
|
{
|
|
|
|
|
entry.State = EntityState.Unchanged;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-07 14:47:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Delete<T>(T entity) where T:class
|
|
|
|
|
{
|
2020-10-22 14:59:36 +08:00
|
|
|
|
_context.Set<T>().Remove(entity);
|
2018-05-07 14:47:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 实现按需要只更新部分更新
|
|
|
|
|
/// <para>如:Update(u =>u.Id==1,u =>new User{Name="ok"});</para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="where">The where.</param>
|
|
|
|
|
/// <param name="entity">The entity.</param>
|
|
|
|
|
public void Update<T>(Expression<Func<T, bool>> where, Expression<Func<T, T>> entity) where T:class
|
|
|
|
|
{
|
2020-10-22 14:59:36 +08:00
|
|
|
|
_context.Set<T>().Where(where).Update(entity);
|
2018-05-07 14:47:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void Delete<T>(Expression<Func<T, bool>> exp) where T : class
|
|
|
|
|
{
|
2020-10-22 14:59:36 +08:00
|
|
|
|
_context.Set<T>().RemoveRange(Filter(exp));
|
2018-05-07 14:47:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Save()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2020-10-22 14:59:36 +08:00
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"{nameof(Save)} validation exception: {exc?.Message}");
|
|
|
|
|
throw (exc.InnerException as Exception ?? exc);
|
2018-05-07 14:47:16 +08:00
|
|
|
|
}
|
2020-10-22 14:59:36 +08:00
|
|
|
|
catch (Exception ex) //DbUpdateException
|
2018-05-07 14:47:16 +08:00
|
|
|
|
{
|
2020-10-22 14:59:36 +08:00
|
|
|
|
throw (ex.InnerException as Exception ?? ex);
|
2018-05-07 14:47:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IQueryable<T> Filter<T>(Expression<Func<T, bool>> exp) where T : class
|
|
|
|
|
{
|
2020-10-22 14:59:36 +08:00
|
|
|
|
var dbSet = _context.Set<T>().AsNoTracking().AsQueryable();
|
2018-05-07 14:47:16 +08:00
|
|
|
|
if (exp != null)
|
|
|
|
|
dbSet = dbSet.Where(exp);
|
|
|
|
|
return dbSet;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-22 14:59:36 +08:00
|
|
|
|
public int ExecuteSql(string sql)
|
2018-03-15 17:36:41 +08:00
|
|
|
|
{
|
2020-10-22 14:59:36 +08:00
|
|
|
|
return _context.Database.ExecuteSqlRaw(sql);
|
2018-05-07 14:47:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-22 14:59:36 +08:00
|
|
|
|
public IQueryable<T> FromSql<T>(string sql, params object[] parameters) where T : class
|
|
|
|
|
{
|
|
|
|
|
return _context.Set<T>().FromSqlRaw(sql, parameters);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IQueryable<T> Query<T>(string sql, params object[] parameters) where T : class
|
|
|
|
|
{
|
|
|
|
|
return _context.Query<T>().FromSqlRaw(sql, parameters);
|
|
|
|
|
}
|
2018-05-07 14:47:16 +08:00
|
|
|
|
}
|
|
|
|
|
}
|