219 lines
6.3 KiB
C#
219 lines
6.3 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Mini.Common;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data.SqlClient;
|
|
using System.Linq;
|
|
using System.Linq.Expressions;
|
|
using System.Text;
|
|
|
|
namespace Mini.Model
|
|
{
|
|
public class EfRepository<T> : EfRepositoryBase<T> where T : BaseEntity
|
|
{
|
|
private readonly DbContext _context;
|
|
private DbSet<T> _entities;
|
|
protected EfRepository(DbContext context)
|
|
{
|
|
this._context = new crmContext();
|
|
}
|
|
|
|
protected virtual DbSet<T> Entities
|
|
{
|
|
get
|
|
{
|
|
if (_entities == null)
|
|
_entities = _context.Set<T>();
|
|
return _entities;
|
|
}
|
|
}
|
|
|
|
public override T Get(Expression<Func<T, bool>> @where)
|
|
{
|
|
return this.Entities.FirstOrDefault(where);
|
|
}
|
|
|
|
public override IQueryable<T> GetList()
|
|
{
|
|
return GetList(null);
|
|
}
|
|
|
|
public override IQueryable<T> GetList(Expression<Func<T, bool>> @where)
|
|
{
|
|
if (where != null)
|
|
{
|
|
return this.Entities.Where(where).AsNoTracking();
|
|
}
|
|
return this.Entities;
|
|
}
|
|
|
|
public override IQueryable<T> GetList<TOrderBy>(Expression<Func<T, bool>> @where, Expression<Func<T, TOrderBy>> orderBy = null, SortOrder sortOrder = SortOrder.Descending)
|
|
{
|
|
if (orderBy == null)
|
|
{
|
|
return this.Entities.Where(where);
|
|
}
|
|
else
|
|
{
|
|
if (sortOrder == SortOrder.Descending)
|
|
return this.Entities.Where(where).OrderByDescending(orderBy);
|
|
else
|
|
return this.Entities.Where(where).OrderBy(orderBy);
|
|
}
|
|
}
|
|
|
|
public override IQueryable<T> GetList<TOrderBy>(Expression<Func<T, TOrderBy>> orderBy, int pageindex, int pagesize, out int totalRecords, SortOrder sortOrder = SortOrder.Descending)
|
|
{
|
|
return GetList(null, orderBy, pageindex, pagesize, out totalRecords, sortOrder);
|
|
}
|
|
|
|
public override IQueryable<T> GetList<TOrderBy>(Expression<Func<T, bool>> @where, Expression<Func<T, TOrderBy>> orderBy, int pageindex, int pagesize, out int totalRecords, SortOrder sortOrder = SortOrder.Descending)
|
|
{
|
|
if (pageindex < 1)
|
|
throw new ArgumentException("pageindex");
|
|
if (pagesize < 1)
|
|
throw new ArgumentException("pagesize");
|
|
|
|
int skip = (pageindex - 1) * pagesize;
|
|
var _query = this.Entities.AsQueryable().AsNoTracking();
|
|
if (null != where)
|
|
_query = _query.Where(where);
|
|
|
|
totalRecords = _query.Count();
|
|
if (sortOrder == SortOrder.Descending)
|
|
{
|
|
_query = _query.OrderByDescending(orderBy);
|
|
}
|
|
else
|
|
_query = _query.OrderBy(orderBy);
|
|
return _query.Skip(skip).Take(pagesize);
|
|
}
|
|
|
|
public override IQueryable<T> GetList<TOrderBy>(Expression<Func<T, bool>> @where, Expression<Func<T, TOrderBy>> orderBy, Pager pg, SortOrder sortOrder = SortOrder.Descending)
|
|
{
|
|
int totalRecords;
|
|
var list = GetList(where, orderBy, pg.page, pg.rows, out totalRecords, sortOrder);
|
|
pg.totalRows = totalRecords;
|
|
return list;
|
|
}
|
|
|
|
public override int Add(T entity)
|
|
{
|
|
try
|
|
{
|
|
if (entity == null)
|
|
throw new ArgumentNullException();
|
|
|
|
this.Entities.Add(entity);
|
|
|
|
return this._context.SaveChanges();
|
|
}
|
|
catch (Exception dbEx)
|
|
{
|
|
throw dbEx;
|
|
}
|
|
}
|
|
|
|
public override void AddList(IEnumerable<T> entities)
|
|
{
|
|
try
|
|
{
|
|
if (entities == null)
|
|
throw new ArgumentNullException();
|
|
|
|
foreach (var entity in entities)
|
|
this.Entities.Add(entity);
|
|
|
|
this._context.SaveChanges();
|
|
}
|
|
catch (Exception dbEx)
|
|
{
|
|
throw dbEx;
|
|
}
|
|
}
|
|
|
|
public override bool Update(T entity)
|
|
{
|
|
try
|
|
{
|
|
if (entity == null)
|
|
throw new ArgumentNullException();
|
|
|
|
return this._context.SaveChanges() > 0;
|
|
}
|
|
catch (Exception dbEx)
|
|
{
|
|
throw dbEx;
|
|
}
|
|
}
|
|
|
|
public override void Update(IEnumerable<T> entities)
|
|
{
|
|
try
|
|
{
|
|
if (entities == null)
|
|
throw new ArgumentNullException();
|
|
|
|
this._context.SaveChanges();
|
|
}
|
|
catch (Exception dbEx)
|
|
{
|
|
throw dbEx;
|
|
}
|
|
}
|
|
|
|
public override void Delete(T entity)
|
|
{
|
|
try
|
|
{
|
|
if (entity == null)
|
|
throw new ArgumentNullException();
|
|
|
|
this.Entities.Remove(entity);
|
|
|
|
this._context.SaveChanges();
|
|
}
|
|
catch (Exception dbEx)
|
|
{
|
|
throw dbEx;
|
|
}
|
|
}
|
|
|
|
public override void Delete(IEnumerable<T> entities)
|
|
{
|
|
try
|
|
{
|
|
if (entities == null)
|
|
throw new ArgumentNullException();
|
|
|
|
foreach (var entity in entities)
|
|
this.Entities.Remove(entity);
|
|
|
|
this._context.SaveChanges();
|
|
}
|
|
catch (Exception dbEx)
|
|
{
|
|
throw dbEx;
|
|
}
|
|
}
|
|
|
|
public override IQueryable<T> Table
|
|
{
|
|
get
|
|
{
|
|
return this.Entities;
|
|
}
|
|
}
|
|
|
|
|
|
//protected string GetFullErrorText(Exception exc)
|
|
//{
|
|
// var msg = string.Empty;
|
|
// foreach (var validationErrors in exc.EntityValidationErrors)
|
|
// foreach (var error in validationErrors.ValidationErrors)
|
|
// msg += string.Format("Property: {0} Error: {1}", error.PropertyName, error.ErrorMessage) + Environment.NewLine;
|
|
// return msg;
|
|
//}
|
|
}
|
|
}
|