252 lines
6.6 KiB
C#
252 lines
6.6 KiB
C#
using IRaCIS.Core.Domain;
|
||
using IRaCIS.Core.Domain.Models;
|
||
//切换.net core 切换注释
|
||
using Microsoft.EntityFrameworkCore;
|
||
using Microsoft.EntityFrameworkCore.ChangeTracking;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Linq.Expressions;
|
||
using System.Threading.Tasks;
|
||
using Z.EntityFramework.Plus;
|
||
|
||
namespace IRaCIS.Core.Infra.EFCore
|
||
{
|
||
public class BaseRepository<T> : IRepository<T> where T : Entity
|
||
{
|
||
protected readonly IRaCISDBContext _dbContext;
|
||
|
||
private readonly DbSet<T> _dbSet;
|
||
|
||
public BaseRepository(IRaCISDBContext db)
|
||
{
|
||
this._dbContext = db;
|
||
_dbSet = db.Set<T>();
|
||
}
|
||
|
||
#region 添加
|
||
|
||
public async ValueTask<EntityEntry<T>> AddAsync(T entity)
|
||
{
|
||
return await _dbSet.AddAsync(entity);
|
||
}
|
||
|
||
public T Add(T t)
|
||
{
|
||
//t.Id = Guid.NewGuid();
|
||
_dbSet.Add(t);
|
||
return t;
|
||
}
|
||
|
||
public async Task AddOrUpdateAsync(T t)
|
||
{
|
||
var exist = await GetAll().FirstOrDefaultAsync(s => s.Id == t.Id);
|
||
if (exist == null)
|
||
{
|
||
Add(t);
|
||
}
|
||
else
|
||
{
|
||
Update(t);
|
||
|
||
}
|
||
await SaveChangesAsync();
|
||
|
||
#region 解决方式一 保存以后的实体会默认自动跟踪 在此detach 以免第二次 更新时 同一个实体被跟踪两次
|
||
|
||
_dbContext.Entry(t).State = EntityState.Detached;
|
||
|
||
#endregion
|
||
|
||
|
||
}
|
||
|
||
public IEnumerable<T> AddRange(IEnumerable<T> t)
|
||
{
|
||
//foreach (var entity in t)
|
||
//{
|
||
// entity.Id = Guid.NewGuid();
|
||
//}
|
||
_dbSet.AddRange(t);
|
||
return t;
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 删除
|
||
public async Task<int> DeleteAsync(Expression<Func<T, bool>> whereLambda)
|
||
{
|
||
return await _dbSet.Where(whereLambda).DeleteAsync();
|
||
}
|
||
|
||
public void Delete(T t)
|
||
{
|
||
_dbSet.Attach(t);
|
||
_dbSet.Remove(t);
|
||
}
|
||
|
||
public bool Delete(Expression<Func<T, bool>> exp)
|
||
{
|
||
return _dbSet.Where(exp).DeleteFromQuery() > 0;
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 更新
|
||
|
||
public async Task<int> UpdateAsync(Expression<Func<T, bool>> whereLambda, Expression<Func<T, T>> entity)
|
||
{
|
||
return await _dbSet.Where(whereLambda).UpdateAsync(entity);
|
||
}
|
||
|
||
public void Update(T t)
|
||
{
|
||
//var a = _dbContext.ChangeTracker.Entries().ToList();
|
||
var entry = this._dbContext.Entry(t);
|
||
|
||
entry.State = EntityState.Modified;
|
||
}
|
||
|
||
/// <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 bool Update(Expression<Func<T, bool>> where, Expression<Func<T, T>> entity)
|
||
{
|
||
return _dbSet.Where(where).UpdateFromQuery(entity) > 0;
|
||
//return _dbSet.Where(where).Update(entity);
|
||
}
|
||
|
||
|
||
#endregion
|
||
|
||
|
||
#region 是否存在
|
||
|
||
public async Task<bool> IsExistAsync(Expression<Func<T, bool>> whereLambda)
|
||
{
|
||
return await _dbSet.AnyAsync(whereLambda);
|
||
}
|
||
|
||
|
||
public bool IsExist(Expression<Func<T, bool>> exp)
|
||
{
|
||
return _dbSet.Any(exp);
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 查找单个对象
|
||
|
||
public async Task<T> GetFirstOrDefaultAsync(Expression<Func<T, bool>> whereLambda)
|
||
{
|
||
return await _dbSet.AsNoTracking().FirstOrDefaultAsync(whereLambda);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 查找单个,且不被上下文所跟踪
|
||
/// </summary>
|
||
public T FindSingleOrDefault(Expression<Func<T, bool>> exp = null)
|
||
{
|
||
return GetAll().FirstOrDefault(exp);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 跟踪
|
||
/// </summary>
|
||
/// <param name="exp"></param>
|
||
/// <returns></returns>
|
||
public T FindSingleOrDefaultTrack(Expression<Func<T, bool>> exp = null)
|
||
{
|
||
return _dbSet.FirstOrDefault(exp);
|
||
}
|
||
|
||
#endregion
|
||
|
||
|
||
|
||
#region 获取总数
|
||
|
||
public Task<int> GetCountAsync(Expression<Func<T, bool>> whereLambda = null)
|
||
{
|
||
return whereLambda == null ? _dbSet.CountAsync() : _dbSet.CountAsync(whereLambda);
|
||
}
|
||
|
||
public int GetCount(Expression<Func<T, bool>> whereLambda = null)
|
||
{
|
||
return whereLambda == null ? _dbSet.Count() : _dbSet.Count(whereLambda);
|
||
}
|
||
|
||
|
||
#endregion
|
||
|
||
|
||
|
||
public IQueryable<T> Find(Expression<Func<T, bool>> exp = null)
|
||
{
|
||
if (exp != null)
|
||
|
||
return _dbSet.AsNoTracking().Where(exp);
|
||
else
|
||
{
|
||
return _dbSet.AsNoTracking();
|
||
|
||
}
|
||
}
|
||
|
||
public IQueryable<T> Find<type>(int pageSize, int pageIndex, bool isAsc,
|
||
Expression<Func<T, type>> orderByLambda, Expression<Func<T, bool>> whereLambda)
|
||
{
|
||
if (orderByLambda == null)
|
||
{
|
||
return Find(whereLambda).Skip((pageIndex - 1) * pageSize).Take(pageSize);
|
||
}
|
||
else
|
||
{
|
||
//是否升序
|
||
if (isAsc)
|
||
{
|
||
|
||
return Find(whereLambda).OrderBy(orderByLambda).Skip((pageIndex - 1) * pageSize).Take(pageSize);
|
||
}
|
||
else
|
||
{
|
||
return Find(whereLambda).OrderByDescending(orderByLambda).Skip((pageIndex - 1) * pageSize).Take(pageSize);
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
public IQueryable<T> Find(int pageSize, int pageIndex, bool isAsc, string propName, Expression<Func<T, bool>> whereLambda)
|
||
{
|
||
//是否升序
|
||
if (isAsc)
|
||
{
|
||
|
||
return GetAll().Where(whereLambda).OrderBy(propName).Skip((pageIndex - 1) * pageSize).Take(pageSize);
|
||
}
|
||
|
||
return GetAll().Where(whereLambda).OrderByDescending(propName).Skip((pageIndex - 1) * pageSize).Take(pageSize);
|
||
|
||
}
|
||
|
||
|
||
public bool SaveChanges()
|
||
{
|
||
return _dbContext.SaveChanges() > 0;
|
||
}
|
||
|
||
|
||
public async Task<int> SaveChangesAsync()
|
||
{
|
||
return await _dbContext.SaveChangesAsync();
|
||
}
|
||
|
||
public IQueryable<T> GetAll()
|
||
{
|
||
return _dbSet.AsNoTracking();
|
||
}
|
||
}
|
||
} |