using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.Entity;
using System.Linq.Expressions;
using System.Reflection;
using Model;
namespace DAL
{
public class UnitOfWork : IDisposable
{
XMinCMSEntities context = new XMinCMSEntities();
//T_ATTACHMENT
private GenericRepository<sysmenu> t_sysmenu_Repository;
public GenericRepository<sysmenu> T_sysmenu_Repository
{
get
{
if (this.t_sysmenu_Repository == null)
{
this.t_sysmenu_Repository = new GenericRepository<sysmenu>(context);
}
return t_sysmenu_Repository;
}
}
public void Save()
{
context.SaveChanges();
}
private bool disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
context.Dispose();
}
}
this.disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
public class SortExpression<TEntity, TType>
{
Expression<Func<TEntity, TType>> SortProperty;
}
public class GenericRepository<TEntity> where TEntity : class
{
internal XMinCMSEntities context;
internal DbSet<TEntity> dbSet;
public GenericRepository(XMinCMSEntities context)
{
this.context = context;
this.dbSet = context.Set<TEntity>();
}
public IQueryable<TEntity> GetAll()
{
return dbSet.AsQueryable();
}
public IQueryable<TEntity> GetPaged(out int total, Expression<Func<TEntity, bool>> filter = null, Func<IQueryable<TEntity>,
IOrderedQueryable<TEntity>> orderBy = null, string includeProperties = "", int index = 0, int size = 50)
{
int skipCount = index * size;
var _reset = Get(filter, orderBy, includeProperties);
_reset = skipCount == 0 ? _reset.Take(size) : _reset.Skip(skipCount).Take(size);
total = _reset.Count();
return _reset.AsQueryable();
}
public IQueryable<TEntity> Get(Expression<Func<TEntity, bool>> filter = null, Func<IQueryable<TEntity>,
IOrderedQueryable<TEntity>> orderBy = null, string includeProperties = "")
{
IQueryable<TEntity> query = dbSet;
if (filter != null)
{
query = query.Where(filter);
}
foreach (var includeProperty in includeProperties.Split
(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
query = query.Include(includeProperty);
}
if (orderBy != null)
{
return orderBy(query).AsQueryable();
}
else
{
return query.AsQueryable();
}
}
public IQueryable<TEntity> Filter(Expression<Func<TEntity, bool>> predicate)
{
return dbSet.Where(predicate).AsQueryable<TEntity>();
}
public IQueryable<TEntity> Filter<Key>(Expression<Func<TEntity, bool>> filter, out int total, int index = 0, int size = 50)
{
int skipCount = index * size;
var _resetSet = filter != null ? dbSet.Where(filter).AsQueryable() : dbSet.AsQueryable();
_resetSet = skipCount == 0 ? _resetSet.Take(size) : _resetSet.Skip(skipCount).Take(size);
total = _resetSet.Count();
return _resetSet.AsQueryable();
}
#region 4.0 查询方法 +GetList<Tkey>(Expression<Func<Tclass, bool>> strWhere, Expression<Func<Tclass, Tkey>> strOrederBy = null, bool order = true)
/// <summary>
/// 4.0 查询方法
/// </summary>
/// <typeparam name="Tkey">如果strOrederBy为null Tkey也可以DBNull</typeparam>
/// <param name="strWhere">查询条件</param>
/// <param name="SelectDelData">查询的结果集中 是否包括 已经软删除的数据</param>
/// <param name="strOrederBy">排序条件</param>
/// <param name="order">是否升序</param>
/// <param name="tableName">连接查询 的表名</param>
/// <returns></returns>
public IQueryable<TEntity> GetList(Expression<Func<TEntity, bool>> strWhere, bool SelectDelData = false, bool isAsNoTracking = true, string tableName = null)
{
Expression<Func<TEntity, bool>> exp = strWhere;
//if (!SelectDelData)
// AddLinq.And(exp, GetWhereIsDel());
IQueryable<TEntity> t = null;
if (isAsNoTracking)
{
if (string.IsNullOrEmpty(tableName))
t = dbSet.AsNoTracking().Where(exp);
else
t = dbSet.Include(tableName).AsNoTracking().Where(exp);
}
else
{
if (string.IsNullOrEmpty(tableName))
t = dbSet.Where(exp);
else
t = dbSet.Include(tableName).Where(exp);
}
return t;
}
/// <summary>
/// 4.0.2 查询方法
/// </summary>
/// <typeparam name="Tkey">如果strOrederBy为null Tkey也可以DBNull</typeparam>
/// <param name="strWhere">查询条件</param>
/// <param name="SelectDelData">查询的结果集中 是否包括 已经软删除的数据</param>
/// <param name="strOrederBy">排序条件</param>
/// <param name="order">是否升序</param>
/// <param name="tableName">连接查询 的表名</param>
/// <returns></returns>
public IQueryable<TEntity> GetList<Key>(Expression<Func<TEntity, bool>> strWhere, bool SelectDelData = false, bool isAsNoTracking = true, Expression<Func<TEntity, Key>> tableName = null)
{
Expression<Func<TEntity, bool>> exp = strWhere;
//if (!SelectDelData)
// exp = AddLinq.And(exp, GetWhereIsDel());//合并 排除一删除查询条件
IQueryable<TEntity> t = null;
if (isAsNoTracking)
if (null == tableName)
t = dbSet.AsNoTracking().Where(exp);
else
t = dbSet.Include(tableName).AsNoTracking().Where(exp);
else
if (null == tableName)
t = dbSet.Where(exp);
else
t = dbSet.Include(tableName).Where(exp);
return t;
}
#endregion
#region 4.1 查询方法2 分页查询 +GetList<Tkey>(int indexPage, int sizePage, Expression<Func<Tclass, bool>> strWhere, Expression<Func<Tclass, Tkey>> strOrederBy = null, bool order = true)
/// <summary>
/// 4.1 查询方法2 分页查询
/// </summary>
/// <typeparam name="Tkey">如果strOrederBy为null Tkey也可以DBNull</typeparam>
/// <param name="indexPage">页码(从1开始)</param>
/// <param name="sizePage">页容量</param>
/// <param name="total">总页数</param>
/// <param name="strWhere">查询条件</param>
/// <param name="SelectDelData">查询的结果集中 是否包括 已经软删除的数据</param>
/// <param name="strOrederBy">排序字段</param>
/// <param name="order">是否升序</param>
/// <param name="tableName">连接查询 的表名</param>
/// <returns></returns>
public IQueryable<TEntity> GetList<Tkey>(int indexPage, int sizePage, out int total, Expression<Func<TEntity, bool>> strWhere, bool SelectDelData = false, Expression<Func<TEntity, Tkey>> strOrederBy = null, bool order = true, bool isAsNoTracking = true, string tableName = null)
{
Expression<Func<TEntity, bool>> exp = strWhere;
//if (!SelectDelData)
// exp = AddLinq.And(exp, GetWhereIsDel());//合并 排除一删除查询条件
IQueryable<TEntity> t = null;
if (isAsNoTracking)
if (string.IsNullOrEmpty(tableName))
t = dbSet.AsNoTracking().Where(exp);
else
t = dbSet.Include(tableName).AsNoTracking().Where(exp);
else
if (string.IsNullOrEmpty(tableName))
t = dbSet.Where(exp);
else
t = dbSet.Include(tableName).Where(exp);
if (strOrederBy != null)
{
if (order)
t = t.OrderBy(strOrederBy);
else
t = t.OrderByDescending(strOrederBy);
}
int count = t.Count();
total = count / sizePage + (count % sizePage > 0 ? 1 : 0);
return t.Skip((indexPage - 1) * sizePage).Take(sizePage);
}
/// <summary>
/// 4.1.2 查询方法2 分页查询
/// </summary>
/// <typeparam name="Tkey">如果strOrederBy为null Tkey也可以DBNull</typeparam>
/// <param name="indexPage">页码(从1开始)</param>
/// <param name="sizePage">页容量</param>
/// <param name="total">总页数</param>
/// <param name="strWhere">查询条件</param>
/// <param name="SelectDelData">查询的结果集中 是否包括 已经软删除的数据</param>
/// <param name="strOrederBy">排序字段</param>
/// <param name="order">是否升序</param>
/// <param name="tableName">连接查询 的表名</param>
/// <returns></returns>
public IQueryable<TEntity> GetList<Tkey, TTb>(int indexPage, int sizePage, out int total, Expression<Func<TEntity, bool>> strWhere, bool SelectDelData = false, Expression<Func<TEntity, Tkey>> strOrederBy = null, bool order = true, bool isAsNoTracking = true, Expression<Func<TEntity, TTb>> tableName = null)
{
Expression<Func<TEntity, bool>> exp = strWhere;
//if (!SelectDelData)
// exp = AddLinq.And(exp, GetWhereIsDel());//合并 排除一删除查询条件
IQueryable<TEntity> t = null;
if (isAsNoTracking)
if (null == tableName)
t = dbSet.AsNoTracking().Where(exp);
else
t = dbSet.Include(tableName).AsNoTracking().Where(exp);
else
if (null == tableName)
t = dbSet.Where(exp);
else
t = dbSet.Include(tableName).Where(exp);
if (strOrederBy != null)
{
if (order)
t = t.OrderBy(strOrederBy);
else
t = t.OrderByDescending(strOrederBy);
}
int count = t.Count();
total = count / sizePage + (count % sizePage > 0 ? 1 : 0);
return t.Skip((indexPage - 1) * sizePage).Take(sizePage);
}
#endregion
public virtual TEntity GetByID(object id)
{
return dbSet.Find(id);
}
public virtual void Insert(TEntity entity)
{
dbSet.Add(entity);
}
public virtual void Delete(object id)
{
TEntity entityToDelete = dbSet.Find(id);
Delete(entityToDelete);
}
public virtual void Delete(TEntity entityToDelete)
{
if (context.Entry(entityToDelete).State == EntityState.Detached)
{
dbSet.Attach(entityToDelete);
}
dbSet.Remove(entityToDelete);
}
public virtual void Update(TEntity entityToUpdate)
{
dbSet.Attach(entityToUpdate);
context.Entry(entityToUpdate).State = EntityState.Modified;
}
/// <summary>
/// 有跟踪状态
/// </summary>
/// <param name="query">SQL查询语句,注意ORCALE参数以:为标记,MS SQL以@为标记</param>
/// <param name="parameters">参数</param>
/// <returns></returns>
public virtual IEnumerable<TEntity> GetWithRawSql(string query, params object[] parameters)
{
return dbSet.SqlQuery(query, parameters).ToList();
}
//
/// <summary>
/// 没有跟踪状态
/// </summary>
/// <param name="query"></param>
/// <param name="parameters"></param>
/// <returns></returns>
public virtual IEnumerable<TEntity> GetwhithdbSql(string query, params object[] parameters)
{
return context.Database.SqlQuery<TEntity>(query, parameters);
}
/// <summary>
/// 获取单行数据,多用于统计
/// </summary>
/// <param name="query"></param>
/// <param name="parameters"></param>
/// <returns></returns>
public virtual TEntity GetwhithdbSqlSingle(string query, params object[] parameters)
{
return context.Database.SqlQuery<TEntity>(query, parameters).FirstOrDefault();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.Entity;
using System.Linq.Expressions;
using System.Reflection;
using Model;
namespace DAL
{
public class UnitOfWork : IDisposable
{
XMinCMSEntities context = new XMinCMSEntities();
//T_ATTACHMENT
private GenericRepository<sysmenu> t_sysmenu_Repository;
public GenericRepository<sysmenu> T_sysmenu_Repository
{
get
{
if (this.t_sysmenu_Repository == null)
{
this.t_sysmenu_Repository = new GenericRepository<sysmenu>(context);
}
return t_sysmenu_Repository;
}
}
public void Save()
{
context.SaveChanges();
}
private bool disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
context.Dispose();
}
}
this.disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
public class SortExpression<TEntity, TType>
{
Expression<Func<TEntity, TType>> SortProperty;
}
public class GenericRepository<TEntity> where TEntity : class
{
internal XMinCMSEntities context;
internal DbSet<TEntity> dbSet;
public GenericRepository(XMinCMSEntities context)
{
this.context = context;
this.dbSet = context.Set<TEntity>();
}
public IQueryable<TEntity> GetAll()
{
return dbSet.AsQueryable();
}
public IQueryable<TEntity> GetPaged(out int total, Expression<Func<TEntity, bool>> filter = null, Func<IQueryable<TEntity>,
IOrderedQueryable<TEntity>> orderBy = null, string includeProperties = "", int index = 0, int size = 50)
{
int skipCount = index * size;
var _reset = Get(filter, orderBy, includeProperties);
_reset = skipCount == 0 ? _reset.Take(size) : _reset.Skip(skipCount).Take(size);
total = _reset.Count();
return _reset.AsQueryable();
}
public IQueryable<TEntity> Get(Expression<Func<TEntity, bool>> filter = null, Func<IQueryable<TEntity>,
IOrderedQueryable<TEntity>> orderBy = null, string includeProperties = "")
{
IQueryable<TEntity> query = dbSet;
if (filter != null)
{
query = query.Where(filter);
}
foreach (var includeProperty in includeProperties.Split
(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
query = query.Include(includeProperty);
}
if (orderBy != null)
{
return orderBy(query).AsQueryable();
}
else
{
return query.AsQueryable();
}
}
public IQueryable<TEntity> Filter(Expression<Func<TEntity, bool>> predicate)
{
return dbSet.Where(predicate).AsQueryable<TEntity>();
}
public IQueryable<TEntity> Filter<Key>(Expression<Func<TEntity, bool>> filter, out int total, int index = 0, int size = 50)
{
int skipCount = index * size;
var _resetSet = filter != null ? dbSet.Where(filter).AsQueryable() : dbSet.AsQueryable();
_resetSet = skipCount == 0 ? _resetSet.Take(size) : _resetSet.Skip(skipCount).Take(size);
total = _resetSet.Count();
return _resetSet.AsQueryable();
}
#region 4.0 查询方法 +GetList<Tkey>(Expression<Func<Tclass, bool>> strWhere, Expression<Func<Tclass, Tkey>> strOrederBy = null, bool order = true)
/// <summary>
/// 4.0 查询方法
/// </summary>
/// <typeparam name="Tkey">如果strOrederBy为null Tkey也可以DBNull</typeparam>
/// <param name="strWhere">查询条件</param>
/// <param name="SelectDelData">查询的结果集中 是否包括 已经软删除的数据</param>
/// <param name="strOrederBy">排序条件</param>
/// <param name="order">是否升序</param>
/// <param name="tableName">连接查询 的表名</param>
/// <returns></returns>
public IQueryable<TEntity> GetList(Expression<Func<TEntity, bool>> strWhere, bool SelectDelData = false, bool isAsNoTracking = true, string tableName = null)
{
Expression<Func<TEntity, bool>> exp = strWhere;
//if (!SelectDelData)
// AddLinq.And(exp, GetWhereIsDel());
IQueryable<TEntity> t = null;
if (isAsNoTracking)
{
if (string.IsNullOrEmpty(tableName))
t = dbSet.AsNoTracking().Where(exp);
else
t = dbSet.Include(tableName).AsNoTracking().Where(exp);
}
else
{
if (string.IsNullOrEmpty(tableName))
t = dbSet.Where(exp);
else
t = dbSet.Include(tableName).Where(exp);
}
return t;
}
/// <summary>
/// 4.0.2 查询方法
/// </summary>
/// <typeparam name="Tkey">如果strOrederBy为null Tkey也可以DBNull</typeparam>
/// <param name="strWhere">查询条件</param>
/// <param name="SelectDelData">查询的结果集中 是否包括 已经软删除的数据</param>
/// <param name="strOrederBy">排序条件</param>
/// <param name="order">是否升序</param>
/// <param name="tableName">连接查询 的表名</param>
/// <returns></returns>
public IQueryable<TEntity> GetList<Key>(Expression<Func<TEntity, bool>> strWhere, bool SelectDelData = false, bool isAsNoTracking = true, Expression<Func<TEntity, Key>> tableName = null)
{
Expression<Func<TEntity, bool>> exp = strWhere;
//if (!SelectDelData)
// exp = AddLinq.And(exp, GetWhereIsDel());//合并 排除一删除查询条件
IQueryable<TEntity> t = null;
if (isAsNoTracking)
if (null == tableName)
t = dbSet.AsNoTracking().Where(exp);
else
t = dbSet.Include(tableName).AsNoTracking().Where(exp);
else
if (null == tableName)
t = dbSet.Where(exp);
else
t = dbSet.Include(tableName).Where(exp);
return t;
}
#endregion
#region 4.1 查询方法2 分页查询 +GetList<Tkey>(int indexPage, int sizePage, Expression<Func<Tclass, bool>> strWhere, Expression<Func<Tclass, Tkey>> strOrederBy = null, bool order = true)
/// <summary>
/// 4.1 查询方法2 分页查询
/// </summary>
/// <typeparam name="Tkey">如果strOrederBy为null Tkey也可以DBNull</typeparam>
/// <param name="indexPage">页码(从1开始)</param>
/// <param name="sizePage">页容量</param>
/// <param name="total">总页数</param>
/// <param name="strWhere">查询条件</param>
/// <param name="SelectDelData">查询的结果集中 是否包括 已经软删除的数据</param>
/// <param name="strOrederBy">排序字段</param>
/// <param name="order">是否升序</param>
/// <param name="tableName">连接查询 的表名</param>
/// <returns></returns>
public IQueryable<TEntity> GetList<Tkey>(int indexPage, int sizePage, out int total, Expression<Func<TEntity, bool>> strWhere, bool SelectDelData = false, Expression<Func<TEntity, Tkey>> strOrederBy = null, bool order = true, bool isAsNoTracking = true, string tableName = null)
{
Expression<Func<TEntity, bool>> exp = strWhere;
//if (!SelectDelData)
// exp = AddLinq.And(exp, GetWhereIsDel());//合并 排除一删除查询条件
IQueryable<TEntity> t = null;
if (isAsNoTracking)
if (string.IsNullOrEmpty(tableName))
t = dbSet.AsNoTracking().Where(exp);
else
t = dbSet.Include(tableName).AsNoTracking().Where(exp);
else
if (string.IsNullOrEmpty(tableName))
t = dbSet.Where(exp);
else
t = dbSet.Include(tableName).Where(exp);
if (strOrederBy != null)
{
if (order)
t = t.OrderBy(strOrederBy);
else
t = t.OrderByDescending(strOrederBy);
}
int count = t.Count();
total = count / sizePage + (count % sizePage > 0 ? 1 : 0);
return t.Skip((indexPage - 1) * sizePage).Take(sizePage);
}
/// <summary>
/// 4.1.2 查询方法2 分页查询
/// </summary>
/// <typeparam name="Tkey">如果strOrederBy为null Tkey也可以DBNull</typeparam>
/// <param name="indexPage">页码(从1开始)</param>
/// <param name="sizePage">页容量</param>
/// <param name="total">总页数</param>
/// <param name="strWhere">查询条件</param>
/// <param name="SelectDelData">查询的结果集中 是否包括 已经软删除的数据</param>
/// <param name="strOrederBy">排序字段</param>
/// <param name="order">是否升序</param>
/// <param name="tableName">连接查询 的表名</param>
/// <returns></returns>
public IQueryable<TEntity> GetList<Tkey, TTb>(int indexPage, int sizePage, out int total, Expression<Func<TEntity, bool>> strWhere, bool SelectDelData = false, Expression<Func<TEntity, Tkey>> strOrederBy = null, bool order = true, bool isAsNoTracking = true, Expression<Func<TEntity, TTb>> tableName = null)
{
Expression<Func<TEntity, bool>> exp = strWhere;
//if (!SelectDelData)
// exp = AddLinq.And(exp, GetWhereIsDel());//合并 排除一删除查询条件
IQueryable<TEntity> t = null;
if (isAsNoTracking)
if (null == tableName)
t = dbSet.AsNoTracking().Where(exp);
else
t = dbSet.Include(tableName).AsNoTracking().Where(exp);
else
if (null == tableName)
t = dbSet.Where(exp);
else
t = dbSet.Include(tableName).Where(exp);
if (strOrederBy != null)
{
if (order)
t = t.OrderBy(strOrederBy);
else
t = t.OrderByDescending(strOrederBy);
}
int count = t.Count();
total = count / sizePage + (count % sizePage > 0 ? 1 : 0);
return t.Skip((indexPage - 1) * sizePage).Take(sizePage);
}
#endregion
public virtual TEntity GetByID(object id)
{
return dbSet.Find(id);
}
public virtual void Insert(TEntity entity)
{
dbSet.Add(entity);
}
public virtual void Delete(object id)
{
TEntity entityToDelete = dbSet.Find(id);
Delete(entityToDelete);
}
public virtual void Delete(TEntity entityToDelete)
{
if (context.Entry(entityToDelete).State == EntityState.Detached)
{
dbSet.Attach(entityToDelete);
}
dbSet.Remove(entityToDelete);
}
public virtual void Update(TEntity entityToUpdate)
{
dbSet.Attach(entityToUpdate);
context.Entry(entityToUpdate).State = EntityState.Modified;
}
/// <summary>
/// 有跟踪状态
/// </summary>
/// <param name="query">SQL查询语句,注意ORCALE参数以:为标记,MS SQL以@为标记</param>
/// <param name="parameters">参数</param>
/// <returns></returns>
public virtual IEnumerable<TEntity> GetWithRawSql(string query, params object[] parameters)
{
return dbSet.SqlQuery(query, parameters).ToList();
}
//
/// <summary>
/// 没有跟踪状态
/// </summary>
/// <param name="query"></param>
/// <param name="parameters"></param>
/// <returns></returns>
public virtual IEnumerable<TEntity> GetwhithdbSql(string query, params object[] parameters)
{
return context.Database.SqlQuery<TEntity>(query, parameters);
}
/// <summary>
/// 获取单行数据,多用于统计
/// </summary>
/// <param name="query"></param>
/// <param name="parameters"></param>
/// <returns></returns>
public virtual TEntity GetwhithdbSqlSingle(string query, params object[] parameters)
{
return context.Database.SqlQuery<TEntity>(query, parameters).FirstOrDefault();
}
}
}