2021-03-20 14:12:24 +08:00
|
|
|
|
using CC.Yi.IDAL;
|
|
|
|
|
|
using CC.Yi.Model;
|
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2021-03-20 21:56:15 +08:00
|
|
|
|
using Microsoft.EntityFrameworkCore.ChangeTracking;
|
2021-03-20 14:12:24 +08:00
|
|
|
|
using System;
|
2021-03-20 21:56:15 +08:00
|
|
|
|
using System.Collections.Generic;
|
2021-03-20 14:12:24 +08:00
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Linq.Expressions;
|
|
|
|
|
|
|
|
|
|
|
|
namespace CC.Yi.DAL
|
|
|
|
|
|
{
|
|
|
|
|
|
public class BaseDal<T> : IBaseDal<T> where T : class, new()
|
|
|
|
|
|
{
|
2021-03-20 21:56:15 +08:00
|
|
|
|
public DbContext Db
|
2021-03-20 14:12:24 +08:00
|
|
|
|
{
|
2021-03-20 21:56:15 +08:00
|
|
|
|
get { return DbContentFactory.GetCurrentDbContent(); }
|
2021-03-20 14:12:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
public IQueryable<T> GetEntities(Expression<Func<T, bool>> whereLambda)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Db.Set<T>().Where(whereLambda).AsQueryable();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-03-20 15:02:23 +08:00
|
|
|
|
public IQueryable<T> GetAllEntities()
|
|
|
|
|
|
{
|
|
|
|
|
|
return Db.Set<T>().AsQueryable();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public int GetCount(Expression<Func<T, bool>> whereLambda)
|
2021-03-20 14:12:24 +08:00
|
|
|
|
{
|
|
|
|
|
|
return Db.Set<T>().Where(whereLambda).Count();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IQueryable<IGrouping<S, T>> GetGroup<S>(Expression<Func<T, bool>> whereLambda, Expression<Func<T, S>> groupByLambda) //分组
|
|
|
|
|
|
{
|
|
|
|
|
|
return Db.Set<T>().Where(whereLambda).GroupBy<T, S>(groupByLambda).AsQueryable();
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public IQueryable<T> GetPageEntities<S>(int pageSize, int pageIndex, out int total, Expression<Func<T, bool>> whereLambda, Expression<Func<T, S>> orderByLambda, bool isAsc)
|
|
|
|
|
|
{
|
|
|
|
|
|
total = Db.Set<T>().Where(whereLambda).Count();
|
|
|
|
|
|
if (isAsc)
|
|
|
|
|
|
{
|
|
|
|
|
|
var pageData = Db.Set<T>().Where(whereLambda)
|
|
|
|
|
|
.OrderBy<T, S>(orderByLambda)
|
|
|
|
|
|
.Skip(pageSize * (pageIndex - 1))
|
|
|
|
|
|
.Take(pageSize).AsQueryable();
|
|
|
|
|
|
return pageData;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
var pageData = Db.Set<T>().Where(whereLambda)
|
|
|
|
|
|
.OrderByDescending<T, S>(orderByLambda)
|
|
|
|
|
|
.Skip(pageSize * (pageIndex - 1))
|
|
|
|
|
|
.Take(pageSize).AsQueryable();
|
|
|
|
|
|
return pageData;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public T Add(T entity)
|
|
|
|
|
|
{
|
|
|
|
|
|
Db.Set<T>().Add(entity);
|
|
|
|
|
|
//Db.SaveChanges();
|
|
|
|
|
|
return entity;
|
|
|
|
|
|
}
|
2021-03-20 21:56:15 +08:00
|
|
|
|
public bool AddRange(IEnumerable<T> entities)
|
|
|
|
|
|
{
|
|
|
|
|
|
Db.Set<T>().AddRange(entities);
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2021-03-20 14:12:24 +08:00
|
|
|
|
|
|
|
|
|
|
public bool Update(T entity)
|
|
|
|
|
|
{
|
|
|
|
|
|
//所有字段均修改
|
|
|
|
|
|
Db.Entry<T>(entity).State = EntityState.Modified;
|
|
|
|
|
|
//return Db.SaveChanges() > 0;
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2021-03-20 21:56:15 +08:00
|
|
|
|
public bool Update(T entity, params string[] propertyNames)
|
|
|
|
|
|
{
|
|
|
|
|
|
EntityEntry entry = Db.Entry<T>(entity);
|
|
|
|
|
|
entry.State = EntityState.Unchanged;
|
|
|
|
|
|
foreach (var item in propertyNames)
|
|
|
|
|
|
{
|
|
|
|
|
|
entry.Property(item).IsModified = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2021-03-20 14:12:24 +08:00
|
|
|
|
|
|
|
|
|
|
public bool Delete(T entity)
|
|
|
|
|
|
{
|
|
|
|
|
|
Db.Entry<T>(entity).State = EntityState.Deleted;
|
|
|
|
|
|
//return Db.SaveChanges() > 0;
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
public bool Detete(int id)
|
|
|
|
|
|
{
|
|
|
|
|
|
var entity = Db.Set<T>().Find(id);//根据id找到实体
|
|
|
|
|
|
Db.Set<T>().Remove(entity);//由于这里先Find找到了实体,所以这里可以用Remove标记该实体要移除(删除)。如果不是先Find到实体就需要用System.Data.Entity.EntityState.Deleted
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2021-03-20 21:56:15 +08:00
|
|
|
|
public bool DeteteRange(IEnumerable<T> entity)
|
|
|
|
|
|
{
|
|
|
|
|
|
Db.Set<T>().RemoveRange(entity);
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-03-20 14:12:24 +08:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|