using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq.Expressions;
using System.Collections;
using Central.Control.Domain;
using Ant.Service.Common;
namespace MES.Production.Service
{
///
/// 数据操作基本实现类,公用实现方法
/// add 作者: 季健国 QQ:181589805 by 2016-05-10
///
/// 具体操作的实体模型
public abstract class RepositoryBase : IRepository where T : class
{
#region 固定公用帮助,含事务
private DbContext context = new MyConfig().db;
///
/// 数据上下文--->根据Domain实体模型名称进行更改
///
public DbContext Context
{
get
{
context.Configuration.ValidateOnSaveEnabled = false;
return context;
}
}
///
/// 数据上下文--->拓展属性
///
public MyConfig Config
{
get
{
return new MyConfig();
}
}
///
/// 公用泛型处理属性
/// 注:所有泛型操作的基础
///
public DbSet dbSet
{
get { return this.Context.Set(); }
}
///
/// 事务
///
private DbContextTransaction _transaction = null;
///
/// 开始事务
///
public DbContextTransaction Transaction
{
get
{
if (this._transaction == null)
{
this._transaction = this.Context.Database.BeginTransaction();
}
return this._transaction;
}
set { this._transaction = value; }
}
///
/// 事务状态
///
public bool Committed { get; set; }
///
/// 异步锁定
///
private readonly object sync = new object();
///
/// 提交事务
///
public void Commit()
{
if (!Committed)
{
lock (sync)
{
if (this._transaction != null)
_transaction.Commit();
}
Committed = true;
}
}
///
/// 回滚事务
///
public void Rollback()
{
Committed = false;
if (this._transaction != null)
this._transaction.Rollback();
}
#endregion
#region 获取单条记录
///
/// 通过lambda表达式获取一条记录p=>p.id==id
///
public virtual T Get(Expression> predicate)
{
try
{
return dbSet.AsNoTracking().SingleOrDefault(predicate);
}
catch (Exception e)
{
throw e;
}
}
#endregion
#region 增删改操作
///
/// 添加一条模型记录,自动提交更改
///
public virtual bool Save(T entity)
{
try
{
var entry = Context.Entry(entity);
entry.State = System.Data.Entity.EntityState.Added;
if (Context.SaveChanges() > 0)
return true;
return false;
}
catch (Exception e)
{
throw e;
}
}
///
/// 添加一条模型记录,自动提交更改
///
public virtual T SaveReturn(T entity)
{
try
{
var entry = Context.Entry(entity);
entry.State = System.Data.Entity.EntityState.Added;
if (Context.SaveChanges() > 0)
return entity;
return entity;
}
catch (Exception e)
{
throw e;
}
}
///
/// 更新一条模型记录,自动提交更改
///
public virtual bool Update(T entity)
{
try
{
var entry = this.Context.Entry(entity);
entry.State = System.Data.Entity.EntityState.Modified;
if (this.Context.SaveChanges() > 0)
return true;
return false;
}
catch (Exception e)
{
throw e;
}
}
///
/// 更新一条模型记录,自动提交更改
///
public virtual T UpdateReturn(T entity)
{
try
{
var entry = this.Context.Entry(entity);
entry.State = System.Data.Entity.EntityState.Modified;
if (this.Context.SaveChanges() > 0)
return entity;
return entity;
}
catch (Exception e)
{
throw e;
}
}
///
/// 更新模型记录,如不存在进行添加操作
///
public virtual T SaveOrUpdateReturn(T entity, bool isEdit)
{
try
{
if (isEdit)
{
return UpdateReturn(entity);
}
else
{
return SaveReturn(entity);
}
//return isEdit ? Update(entity) : Save(entity);
}
catch (Exception e) { throw e; }
}
///
/// 更新模型记录,如不存在进行添加操作
///
public virtual bool SaveOrUpdate(T entity, bool isEdit)
{
try
{
return isEdit ? Update(entity) : Save(entity);
}
catch (Exception e) { throw e; }
}
///
/// 删除一条或多条模型记录,含事务
///
public virtual int Delete(Expression> predicate = null)
{
try
{
int rows = 0;
IQueryable entry = (predicate == null) ? this.dbSet.AsQueryable() : this.dbSet.Where(predicate);
List list = entry.ToList();
if (list.Count > 0)
{
for (int i = 0; i < list.Count; i++)
{
this.dbSet.Remove(list[i]);
}
rows = this.Context.SaveChanges();
}
return rows;
}
catch (Exception e)
{
throw e;
}
}
///
/// 使用原始SQL语句,含事务处理
///
public virtual int DeleteBySql(string sql, params DbParameter[] para)
{
try
{
return this.Context.Database.ExecuteSqlCommand(sql, para);
}
catch (Exception e)
{
throw e;
}
}
#endregion
#region 多模型操作
///
/// 增加多模型数据,指定独立模型集合
///
public virtual int SaveList(List t) where T1 : class
{
try
{
if (t == null || t.Count == 0) return 0;
this.Context.Set().Local.Clear();
foreach (var item in t)
{
this.Context.Set().Add(item);
}
return this.Context.SaveChanges();
}
catch (Exception e)
{
throw e;
}
}
///
/// 增加多模型数据,与当前模型一致
///
public virtual int SaveList(List t)
{
try
{
this.dbSet.Local.Clear();
foreach (var item in t)
{
this.dbSet.Add(item);
}
return this.Context.SaveChanges();
}
catch (Exception e)
{
throw e;
}
}
///
/// 更新多模型,指定独立模型集合
///
public virtual int UpdateList(List t) where T1 : class
{
if (t.Count <= 0) return 0;
try
{
foreach (var item in t)
{
this.Context.Entry(item).State = System.Data.Entity.EntityState.Modified;
}
return this.Context.SaveChanges();
}
catch (Exception e)
{
throw e;
}
}
///
/// 更新多模型,与当前模型一致
///
public virtual int UpdateList(List t)
{
if (t.Count <= 0) return 0;
try
{
foreach (var item in t)
{
this.Context.Entry(item).State = System.Data.Entity.EntityState.Modified;
}
return this.Context.SaveChanges();
}
catch (Exception e) { throw e; }
}
///
/// 批量删除数据,当前模型
///
public virtual int DeleteList(List t)
{
if (t == null || t.Count == 0) return 0;
foreach (var item in t)
{
this.dbSet.Remove(item);
}
return this.Context.SaveChanges();
}
///
/// 批量删除数据,自定义模型
///
public virtual int DeleteList(List t) where T1 : class
{
try
{
if (t == null || t.Count == 0) return 0;
foreach (var item in t)
{
this.Context.Set().Remove(item);
}
return this.Context.SaveChanges();
}
catch (Exception e) { throw e; }
}
#endregion
#region 存储过程操作
///
/// 执行返回影响行数的存储过程
///
/// 过程名称
/// 参数对象
///
public virtual object ExecuteProc(string procname, params DbParameter[] parameter)
{
try
{
return ExecuteSqlCommand(procname, parameter);
}
catch (Exception e)
{
throw e;
}
}
///
/// 执行返回结果集的存储过程
///
/// 过程名称
/// 参数对象
///
public virtual object ExecuteQueryProc(string procname, params DbParameter[] parameter)
{
try
{
return this.Context.Database.SqlFunctionForDynamic(procname, parameter);
}
catch (Exception e)
{
throw e;
}
}
#endregion
#region 存在验证操作
///
/// 验证当前条件是否存在相同项
///
public virtual bool IsExist(Expression> predicate)
{
var entry = this.dbSet.Where(predicate);
return (entry.Any());
}
///
/// 根据SQL验证实体对象是否存在
///
public virtual bool IsExist(string sql, params DbParameter[] para)
{
IEnumerable result = this.Context.Database.SqlQuery(typeof(int), sql, para);
if (result.GetEnumerator().Current == null || result.GetEnumerator().Current.ToString() == "0")
return false;
return true;
}
#endregion
#region 获取多条数据操作
///
/// 返回IQueryable集合,延时加载数据
///
public virtual IQueryable LoadAll(Expression> predicate)
{
try
{
if (predicate != null)
{
return this.dbSet.Where(predicate).AsNoTracking();
}
return this.dbSet.AsQueryable().AsNoTracking();
}
catch (Exception e)
{
throw e;
}
}
///
/// 返回DbQuery集合,延时加载数据
///
public virtual DbQuery LoadQueryAll(Expression> predicate)
{
try
{
if (predicate != null)
{
return this.dbSet.Where(predicate) as DbQuery;
}
return this.dbSet;
}
catch (Exception e)
{
throw e;
}
}
///
/// 返回List集合,不采用延时加载
///
public virtual List LoadListAll(Expression> predicate)
{
try
{
if (predicate != null)
{
return this.dbSet.Where(predicate).AsNoTracking().ToList();
}
return this.dbSet.AsQueryable().AsNoTracking().ToList();
}
catch (Exception e)
{
throw e;
}
}
///
/// 返回IEnumerable集合,采用原始T-Sql方式
///
public virtual IEnumerable LoadEnumerableAll(string sql, params DbParameter[] para)
{
try
{
return this.Context.Database.SqlQuery(sql, para);
}
catch (Exception e)
{
throw e;
}
}
///
/// 返回IEnumerable动态集合,采用原始T-Sql方式
///
public virtual System.Collections.IEnumerable LoadEnumerable(string sql, params DbParameter[] para)
{
try
{
return this.Context.Database.SqlQueryForDynamic(sql, para);
}
catch (Exception e)
{
throw e;
}
}
///
/// 返回IList集合,采用原始T-Sql方式
///
public virtual List SelectBySql(string sql, params DbParameter[] para)
{
try
{
return this.Context.Database.SqlQuery(typeof(T), sql, para).Cast().ToList();
}
catch (Exception e)
{
throw e;
}
}
///
/// 指定泛型,返回IList集合,采用原始T-Sql方式
///
public virtual List SelectBySql(string sql, params DbParameter[] para)
{
try
{
return this.Context.Database.SqlQuery(sql, para).ToList();
}
catch (Exception e)
{
throw e;
}
}
///
/// 可指定返回结果、排序、查询条件的通用查询方法,返回实体对象
///
/// 实体对象
/// 排序字段类型
/// 数据结果,与TEntity一致
/// 过滤条件,需要用到类型转换的需要提前处理与数据表一致的
/// 排序字段
/// 返回结果(必须是模型中存在的字段)
/// 排序方向,true为正序false为倒序
/// 实体集合
public virtual List QueryEntity
(Expression> where,
Expression> orderby,
Expression> selector,
bool IsAsc)
where TEntity : class
where TResult : class
{
IQueryable query = this.Context.Set();
if (where != null)
{
query = query.Where(where);
}
if (orderby != null)
{
query = IsAsc ? query.OrderBy(orderby) : query.OrderByDescending(orderby);
}
if (selector == null)
{
return query.Cast().AsNoTracking().ToList();
}
return query.Select(selector).AsNoTracking().ToList();
}
///
/// 可指定返回结果、排序、查询条件的通用查询方法,返回Object对象
///
/// 实体对象
/// 排序字段类型
/// 过滤条件,需要用到类型转换的需要提前处理与数据表一致的
/// 排序字段
/// 返回结果(必须是模型中存在的字段)
/// 排序方向,true为正序false为倒序
/// 自定义实体集合
public virtual List