【无私分享:ASP.NET CORE 项目实战(第五章)】Repository仓储 UnitofWork

目录索引 

【无私分享:ASP.NET CORE 项目实战】目录索引

简介

  本章我们来创建仓储类Repository 并且引入 UnitOfWork

我对UnitOfWork的一些理解

   UnitOfWork 工作单元,对于这个的解释和实例,网上很多很多大神之作,我在这就不班门弄斧了,只是浅谈 一下个人理解,不一定对,希望大家指正:

  UnitOfWork 看了很多,个人理解就是 统一事务,在很多操作中我们可能是进行多项操作的,比如 同时保存两个表的数据,如果我们先保存一个表(SaveChanges()之后),再保存另一个表(SaveChanges())其实是两个不同的事务,通俗的讲,我们对数据库提交了两次数据。而UnitOfWork 就是 把本次操作(分别保存两张表)放在统一事务中,所有的CRUD完成后,统一提交数据,即保证数据的一致性,又减少了提交次数,提高性能。

  举个例子,我们之前的Repository的保存操作:

  Repository:

  public virtual bool Save(T entity)
  {
    _Context.Add(entity);
    return _Context.SaveChanges() > 0;
  }

  Controller:

  public ActionResult Save()

  {

    var Users=new Users(){Id=1,UserName="张三"};

    var Logs=new Logs(){log="注册了用户张三"};

    _UserService.Save(Users);

    _LogService.Save(Logs);

  }

因为本身EF就是带事务的,但是_Context.SaveChanges()之后,事务就结束了,也提交了数据。所以,上面的例子 应该是开启了两次事务,提交了两次数据(一次是Users 一次是 Logs)

这显然是不合理的,首先不说提交两次数据性能的问题,如果用户注册失败,那么日志也不应该添加,应该回滚。但是,显然,上面没有这么做。

所以有了UnitOfWork。

使用UnitOfWork之后,代码应该是这样的:

  UnitOfWork:

  public bool Commit()
  {
    return _Context.SaveChanges() > 0; 
  }

  Repository:

  public virtual bool Save(T entity)
  {
    _Context.Add(entity);
  }

  Controller:

  public ActionResult Save()

  {

    var Users=new Users(){Id=1,UserName="张三"};

    var Logs=new Logs(){log="注册了用户张三"};

    _UserService.Save(Users);

    _LogService.Save(Logs);

    _UnitOfWork.Commit();

  }

  

UnitOfWork接口和实现类

我们在wkmvc.Core类库下,新建一个接口 IUnitOfWork:

 namespace wkmvc.Core
{
/// <summary>
/// Describe:工作单元接口
/// Author:yuangang
/// Date:2016/07/16
/// Blogs:http://www.cnblogs.com/yuangang
/// </summary>
public interface IUnitOfWork
{
bool Commit();
}
}

  IUnitOfWork 实现类 UnitOfWork:

 using System;
using wkmvc.Data; namespace wkmvc.Core
{
/// <summary>
/// Describe:工作单元实现类
/// Author:yuangang
/// Date:2016/07/16
/// Blogs:http://www.cnblogs.com/yuangang
/// </summary>
public class UnitOfWork : IUnitOfWork, IDisposable
{
#region 数据上下文 /// <summary>
/// 数据上下文
/// </summary>
private ApplicationDbContext _Context;
public UnitOfWork(ApplicationDbContext Context)
{
_Context = Context;
} #endregion public bool Commit()
{
return _Context.SaveChanges() > ;
} public void Dispose()
{
if(_Context!=null)
{
_Context.Dispose();
}
GC.SuppressFinalize(this);
}
}
}

   这样,UnitOfWork 就完成了。下面我们来添加仓储:

仓储 IRepository、Repository

新建接口 IRepository 添加基本的操作方法:

 using System;
using System.Linq.Expressions; namespace wkmvc.Core
{
/// <summary>
/// Describe:仓储接口
/// Author:yuangang
/// Date:2016/07/16
/// Blogs:http://www.cnblogs.com/yuangang
/// </summary>
/// <typeparam name="T">实体模型</typeparam>
public interface IRepository<T> where T : class
{
#region 单模型 CRUD 操作 /// <summary>
/// 增加一条记录
/// </summary>
/// <param name="entity">实体模型</param>
/// <param name="IsCommit">是否提交(默认提交)</param>
/// <returns></returns>
bool Save(T entity, bool IsCommit = true); /// <summary>
/// 更新一条记录
/// </summary>
/// <param name="entity">实体模型</param>
/// <param name="IsCommit">是否提交(默认提交)</param>
/// <returns></returns>
bool Update(T entity, bool IsCommit = true); /// <summary>
/// 增加或更新一条记录
/// </summary>
/// <param name="entity">实体模型</param>
/// <param name="IsSave">是否增加</param>
/// <param name="IsCommit">是否提交(默认提交)</param>
/// <returns></returns>
bool SaveOrUpdate(T entity, bool IsSave, bool IsCommit = true); /// <summary>
/// 通过Lamda表达式获取实体
/// </summary>
/// <param name="predicate">Lamda表达式(p=>p.Id==Id)</param>
/// <returns></returns>
T Get(Expression<Func<T, bool>> predicate); /// <summary>
/// 删除一条记录
/// </summary>
/// <param name="entity">实体模型</param>
/// <param name="IsCommit">是否提交(默认提交)</param>
/// <returns></returns>
bool Delete(T entity, bool IsCommit = true); #endregion
}
}

   实现类 Repository :

 using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
using System.Linq.Expressions;
using wkmvc.Data; namespace wkmvc.Core
{
/// <summary>
/// Describe:仓储实现类
/// Author:yuangang
/// Date:2016/07/16
/// Blogs:http://www.cnblogs.com/yuangang
/// </summary>
/// <typeparam name="T">实体模型</typeparam>
public abstract class Repository<T> : IRepository<T> where T : class
{
#region 数据上下文 /// <summary>
/// 数据上下文
/// </summary>
private ApplicationDbContext _Context; /// <summary>
/// 工作单元
/// </summary>
UnitOfWork _UnitOfWork; public Repository(ApplicationDbContext Context)
{
_Context = Context;
_UnitOfWork = new UnitOfWork(_Context);
} #endregion #region 单模型 CRUD 操作 /// <summary>
/// 增加一条记录
/// </summary>
/// <param name="entity">实体模型</param>
/// <param name="IsCommit">是否提交(默认提交)</param>
/// <returns></returns>
public virtual bool Save(T entity,bool IsCommit=true)
{
_Context.Add(entity);
if (IsCommit)
return _UnitOfWork.Commit();
else
return false;
} /// <summary>
/// 更新一条记录
/// </summary>
/// <param name="entity">实体模型</param>
/// <param name="IsCommit">是否提交(默认提交)</param>
/// <returns></returns>
public virtual bool Update(T entity, bool IsCommit = true)
{
_Context.Attach(entity);
_Context.Entry(entity).State = EntityState.Modified;
if (IsCommit)
return _UnitOfWork.Commit();
else
return false;
} /// <summary>
/// 增加或更新一条记录
/// </summary>
/// <param name="entity">实体模型</param>
/// <param name="IsSave">是否增加</param>
/// <param name="IsCommit">是否提交(默认提交)</param>
/// <returns></returns>
public virtual bool SaveOrUpdate(T entity, bool IsSave, bool IsCommit = true)
{
return IsSave ? Save(entity, IsCommit) : Update(entity, IsCommit);
} /// <summary>
/// 通过Lamda表达式获取实体
/// </summary>
/// <param name="predicate">Lamda表达式(p=>p.Id==Id)</param>
/// <returns></returns>
public virtual T Get(Expression<Func<T, bool>> predicate)
{
return _Context.Set<T>().AsNoTracking().SingleOrDefault(predicate);
} /// <summary>
/// 删除一条记录
/// </summary>
/// <param name="entity">实体模型</param>
/// <param name="IsCommit">是否提交(默认提交)</param>
/// <returns></returns>
public virtual bool Delete(T entity, bool IsCommit = true)
{
if (entity == null) return false;
_Context.Remove(entity); if (IsCommit)
return _UnitOfWork.Commit();
else
return false;
} #endregion }
}

我们都添加了一个  bool IsCommit = true 参数,是为了方便,如果仅仅是进行一项操作,那就没必要使用 UnitOfWork 统一 直接 SaveChanages() 就行了

我们来看下使用:

  //第一种
  public bool TestOne()
  {
    var users = new SYS_USER() { UserName="张三",Account="zhangsan",Password="123456" };

    return _UserService.Save(users);
  }

  //第二种
  public bool TestTwo()
  {
    var users1 = new SYS_USER() { UserName = "张三", Account = "zhangsan", Password = "123456" };
    var users2 = new SYS_USER() { UserName = "李四", Account = "lisi", Password = "456789" };
    var users3 = new SYS_USER() { UserName = "王五", Account = "wangwu", Password = "321654" };

    _UserService.Save(users1);
    _UserService.Save(users2);
    _UserService.Save(users3);

    return _UnitOfWork.Commit();
  }

本篇主要是介绍用法,具体的解释清大家参考大神们的讲解,如有不对的地方希望指正。具体的实现代码,我们后面一起一点一点写。

给大家贴一下个人实现的仓储 IRepository、Repository代码:

IRepository:

     public interface IRepository<T> where T : class
{
#region 单模型 CRUD 操作 /// <summary>
/// 增加一条记录
/// </summary>
/// <param name="entity">实体模型</param>
/// <param name="IsCommit">是否提交(默认提交)</param>
/// <returns></returns>
bool Save(T entity, bool IsCommit = true);
/// <summary>
/// 增加一条记录(异步方式)
/// </summary>
/// <param name="entity">实体模型</param>
/// <param name="IsCommit">是否提交(默认提交)</param>
/// <returns></returns>
Task<bool> SaveAsync(T entity, bool IsCommit = true); /// <summary>
/// 更新一条记录
/// </summary>
/// <param name="entity">实体模型</param>
/// <param name="IsCommit">是否提交(默认提交)</param>
/// <returns></returns>
bool Update(T entity, bool IsCommit = true);
/// <summary>
/// 更新一条记录(异步方式)
/// </summary>
/// <param name="entity">实体模型</param>
/// <param name="IsCommit">是否提交(默认提交)</param>
/// <returns></returns>
Task<bool> UpdateAsync(T entity, bool IsCommit = true); /// <summary>
/// 增加或更新一条记录
/// </summary>
/// <param name="entity">实体模型</param>
/// <param name="IsSave">是否增加</param>
/// <param name="IsCommit">是否提交(默认提交)</param>
/// <returns></returns>
bool SaveOrUpdate(T entity, bool IsSave, bool IsCommit = true);
/// <summary>
/// 增加或更新一条记录(异步方式)
/// </summary>
/// <param name="entity">实体模型</param>
/// <param name="IsSave">是否增加</param>
/// <param name="IsCommit">是否提交(默认提交)</param>
/// <returns></returns>
Task<bool> SaveOrUpdateAsync(T entity, bool IsSave, bool IsCommit = true); /// <summary>
/// 通过Lamda表达式获取实体
/// </summary>
/// <param name="predicate">Lamda表达式(p=>p.Id==Id)</param>
/// <returns></returns>
T Get(Expression<Func<T, bool>> predicate);
/// <summary>
/// 通过Lamda表达式获取实体(异步方式)
/// </summary>
/// <param name="predicate">Lamda表达式(p=>p.Id==Id)</param>
/// <returns></returns>
Task<T> GetAsync(Expression<Func<T, bool>> predicate); /// <summary>
/// 删除一条记录
/// </summary>
/// <param name="entity">实体模型</param>
/// <param name="IsCommit">是否提交(默认提交)</param>
/// <returns></returns>
bool Delete(T entity, bool IsCommit = true);
/// <summary>
/// 删除一条记录(异步方式)
/// </summary>
/// <param name="entity">实体模型</param>
/// <param name="IsCommit">是否提交(默认提交)</param>
/// <returns></returns>
Task<bool> DeleteAsync(T entity, bool IsCommit = true); #endregion #region 多模型 操作 /// <summary>
/// 增加多条记录,同一模型
/// </summary>
/// <param name="T1">实体模型集合</param>
/// <param name="IsCommit">是否提交(默认提交)</param>
/// <returns></returns>
bool SaveList(List<T> T1, bool IsCommit = true);
/// <summary>
/// 增加多条记录,同一模型(异步方式)
/// </summary>
/// <param name="T1">实体模型集合</param>
/// <param name="IsCommit">是否提交(默认提交)</param>
/// <returns></returns>
Task<bool> SaveListAsync(List<T> T1, bool IsCommit = true); /// <summary>
/// 增加多条记录,独立模型
/// </summary>
/// <param name="T1">实体模型集合</param>
/// <param name="IsCommit">是否提交(默认提交)</param>
/// <returns></returns>
bool SaveList<T1>(List<T1> T, bool IsCommit = true) where T1 : class;
/// <summary>
/// 增加多条记录,独立模型(异步方式)
/// </summary>
/// <param name="T1">实体模型集合</param>
/// <param name="IsCommit">是否提交(默认提交)</param>
/// <returns></returns>
Task<bool> SaveListAsync<T1>(List<T1> T, bool IsCommit = true) where T1 : class; /// <summary>
/// 更新多条记录,同一模型
/// </summary>
/// <param name="T1">实体模型集合</param>
/// <param name="IsCommit">是否提交(默认提交)</param>
/// <returns></returns>
bool UpdateList(List<T> T1, bool IsCommit = true);
/// <summary>
/// 更新多条记录,同一模型(异步方式)
/// </summary>
/// <param name="T1">实体模型集合</param>
/// <param name="IsCommit">是否提交(默认提交)</param>
/// <returns></returns>
Task<bool> UpdateListAsync(List<T> T1, bool IsCommit = true); /// <summary>
/// 更新多条记录,独立模型
/// </summary>
/// <param name="T1">实体模型集合</param>
/// <param name="IsCommit">是否提交(默认提交)</param>
/// <returns></returns>
bool UpdateList<T1>(List<T1> T, bool IsCommit = true) where T1 : class;
/// <summary>
/// 更新多条记录,独立模型(异步方式)
/// </summary>
/// <param name="T1">实体模型集合</param>
/// <param name="IsCommit">是否提交(默认提交)</param>
/// <returns></returns>
Task<bool> UpdateListAsync<T1>(List<T1> T, bool IsCommit = true) where T1 : class; /// <summary>
/// 删除多条记录,同一模型
/// </summary>
/// <param name="T1">实体模型集合</param>
/// <param name="IsCommit">是否提交(默认提交)</param>
/// <returns></returns>
bool DeleteList(List<T> T1, bool IsCommit = true);
/// <summary>
/// 删除多条记录,同一模型(异步方式)
/// </summary>
/// <param name="T1">实体模型集合</param>
/// <param name="IsCommit">是否提交(默认提交)</param>
/// <returns></returns>
Task<bool> DeleteListAsync(List<T> T1, bool IsCommit = true); /// <summary>
/// 删除多条记录,独立模型
/// </summary>
/// <param name="T1">实体模型集合</param>
/// <param name="IsCommit">是否提交(默认提交)</param>
/// <returns></returns>
bool DeleteList<T1>(List<T1> T, bool IsCommit = true) where T1 : class;
/// <summary>
/// 删除多条记录,独立模型(异步方式)
/// </summary>
/// <param name="T1">实体模型集合</param>
/// <param name="IsCommit">是否提交(默认提交)</param>
/// <returns></returns>
Task<bool> DeleteListAsync<T1>(List<T1> T, bool IsCommit = true) where T1 : class; /// <summary>
/// 通过Lamda表达式,删除一条或多条记录
/// </summary>
/// <param name="predicate"></param>
/// <param name="IsCommit"></param>
/// <returns></returns>
bool Delete(Expression<Func<T, bool>> predicate, bool IsCommit = true);
/// <summary>
/// 通过Lamda表达式,删除一条或多条记录(异步方式)
/// </summary>
/// <param name="predicate"></param>
/// <param name="IsCommit"></param>
/// <returns></returns>
Task<bool> DeleteAsync(Expression<Func<T, bool>> predicate, bool IsCommit = true); #endregion #region 获取多条数据操作 /// <summary>
/// 返回IQueryable集合,延时加载数据
/// </summary>
/// <param name="predicate"></param>
/// <returns></returns>
IQueryable<T> LoadAll(Expression<Func<T, bool>> predicate);
/// <summary>
/// 返回IQueryable集合,延时加载数据(异步方式)
/// </summary>
/// <param name="predicate"></param>
/// <returns></returns>
Task<IQueryable<T>> LoadAllAsync(Expression<Func<T, bool>> predicate); // <summary>
/// 返回List<T>集合,不采用延时加载
/// </summary>
/// <param name="predicate"></param>
/// <returns></returns>
List<T> LoadListAll(Expression<Func<T, bool>> predicate);
// <summary>
/// 返回List<T>集合,不采用延时加载(异步方式)
/// </summary>
/// <param name="predicate"></param>
/// <returns></returns>
Task<List<T>> LoadListAllAsync(Expression<Func<T, bool>> predicate); /// <summary>
/// T-Sql方式:返回IQueryable<T>集合
/// </summary>
/// <param name="sql">SQL语句</param>
/// <param name="para">Parameters参数</param>
/// <returns></returns>
IQueryable<T> LoadAllBySql(string sql, params DbParameter[] para);
/// <summary>
/// T-Sql方式:返回IQueryable<T>集合(异步方式)
/// </summary>
/// <param name="sql">SQL语句</param>
/// <param name="para">Parameters参数</param>
/// <returns></returns>
Task<IQueryable<T>> LoadAllBySqlAsync(string sql, params DbParameter[] para); /// <summary>
/// T-Sql方式:返回List<T>集合
/// </summary>
/// <param name="sql">SQL语句</param>
/// <param name="para">Parameters参数</param>
/// <returns></returns>
List<T> LoadListAllBySql(string sql, params DbParameter[] para);
/// <summary>
/// T-Sql方式:返回List<T>集合(异步方式)
/// </summary>
/// <param name="sql">SQL语句</param>
/// <param name="para">Parameters参数</param>
/// <returns></returns>
Task<List<T>> LoadListAllBySqlAsync(string sql, params DbParameter[] para); /// <summary>
/// 可指定返回结果、排序、查询条件的通用查询方法,返回实体对象集合
/// </summary>
/// <typeparam name="TEntity">实体对象</typeparam>
/// <typeparam name="TOrderBy">排序字段类型</typeparam>
/// <typeparam name="TResult">数据结果,与TEntity一致</typeparam>
/// <param name="where">过滤条件,需要用到类型转换的需要提前处理与数据表一致的</param>
/// <param name="orderby">排序字段</param>
/// <param name="selector">返回结果(必须是模型中存在的字段)</param>
/// <param name="IsAsc">排序方向,true为正序false为倒序</param>
/// <returns>实体集合</returns>
List<TResult> QueryEntity<TEntity, TOrderBy, TResult>(Expression<Func<TEntity, bool>> where,Expression<Func<TEntity, TOrderBy>> orderby,Expression<Func<TEntity, TResult>> selector,bool IsAsc)
where TEntity : class
where TResult : class;
/// <summary>
/// 可指定返回结果、排序、查询条件的通用查询方法,返回实体对象集合(异步方式)
/// </summary>
/// <typeparam name="TEntity">实体对象</typeparam>
/// <typeparam name="TOrderBy">排序字段类型</typeparam>
/// <typeparam name="TResult">数据结果,与TEntity一致</typeparam>
/// <param name="where">过滤条件,需要用到类型转换的需要提前处理与数据表一致的</param>
/// <param name="orderby">排序字段</param>
/// <param name="selector">返回结果(必须是模型中存在的字段)</param>
/// <param name="IsAsc">排序方向,true为正序false为倒序</param>
/// <returns>实体集合</returns>
Task<List<TResult>> QueryEntityAsync<TEntity, TOrderBy, TResult>(Expression<Func<TEntity, bool>> where, Expression<Func<TEntity, TOrderBy>> orderby, Expression<Func<TEntity, TResult>> selector, bool IsAsc)
where TEntity : class
where TResult : class; /// <summary>
/// 可指定返回结果、排序、查询条件的通用查询方法,返回Object对象集合
/// </summary>
/// <typeparam name="TEntity">实体对象</typeparam>
/// <typeparam name="TOrderBy">排序字段类型</typeparam>
/// <param name="where">过滤条件,需要用到类型转换的需要提前处理与数据表一致的</param>
/// <param name="orderby">排序字段</param>
/// <param name="selector">返回结果(必须是模型中存在的字段)</param>
/// <param name="IsAsc">排序方向,true为正序false为倒序</param>
/// <returns>自定义实体集合</returns>
List<object> QueryObject<TEntity, TOrderBy>(Expression<Func<TEntity, bool>> where,Expression<Func<TEntity, TOrderBy>> orderby,Func<IQueryable<TEntity>,List<object>> selector,bool IsAsc)
where TEntity : class;
/// <summary>
/// 可指定返回结果、排序、查询条件的通用查询方法,返回Object对象集合(异步方式)
/// </summary>
/// <typeparam name="TEntity">实体对象</typeparam>
/// <typeparam name="TOrderBy">排序字段类型</typeparam>
/// <param name="where">过滤条件,需要用到类型转换的需要提前处理与数据表一致的</param>
/// <param name="orderby">排序字段</param>
/// <param name="selector">返回结果(必须是模型中存在的字段)</param>
/// <param name="IsAsc">排序方向,true为正序false为倒序</param>
/// <returns>自定义实体集合</returns>
Task<List<object>> QueryObjectAsync<TEntity, TOrderBy>(Expression<Func<TEntity, bool>> where, Expression<Func<TEntity, TOrderBy>> orderby, Func<IQueryable<TEntity>, List<object>> selector, bool IsAsc)
where TEntity : class; /// <summary>
/// 可指定返回结果、排序、查询条件的通用查询方法,返回动态类对象集合
/// </summary>
/// <typeparam name="TEntity">实体对象</typeparam>
/// <typeparam name="TOrderBy">排序字段类型</typeparam>
/// <param name="where">过滤条件,需要用到类型转换的需要提前处理与数据表一致的</param>
/// <param name="orderby">排序字段</param>
/// <param name="selector">返回结果(必须是模型中存在的字段)</param>
/// <param name="IsAsc">排序方向,true为正序false为倒序</param>
/// <returns>动态类</returns>
dynamic QueryDynamic<TEntity, TOrderBy>(Expression<Func<TEntity, bool>> where,Expression<Func<TEntity, TOrderBy>> orderby, Func<IQueryable<TEntity>,List<object>> selector,bool IsAsc)
where TEntity : class;
/// <summary>
/// 可指定返回结果、排序、查询条件的通用查询方法,返回动态类对象集合(异步方式)
/// </summary>
/// <typeparam name="TEntity">实体对象</typeparam>
/// <typeparam name="TOrderBy">排序字段类型</typeparam>
/// <param name="where">过滤条件,需要用到类型转换的需要提前处理与数据表一致的</param>
/// <param name="orderby">排序字段</param>
/// <param name="selector">返回结果(必须是模型中存在的字段)</param>
/// <param name="IsAsc">排序方向,true为正序false为倒序</param>
/// <returns>动态类</returns>
Task<dynamic> QueryDynamicAsync<TEntity, TOrderBy>(Expression<Func<TEntity, bool>> where, Expression<Func<TEntity, TOrderBy>> orderby, Func<IQueryable<TEntity>, List<object>> selector, bool IsAsc)
where TEntity : class; #endregion #region 验证是否存在 /// <summary>
/// 验证当前条件是否存在相同项
/// </summary>
bool IsExist(Expression<Func<T, bool>> predicate);
/// <summary>
/// 验证当前条件是否存在相同项(异步方式)
/// </summary>
Task<bool> IsExistAsync(Expression<Func<T, bool>> predicate); /// <summary>
/// 根据SQL验证实体对象是否存在
/// </summary>
bool IsExist(string sql, params DbParameter[] para);
/// <summary>
/// 根据SQL验证实体对象是否存在(异步方式)
/// </summary>
Task<bool> IsExistAsync(string sql, params DbParameter[] para); #endregion
}

Repository:

   public abstract class Repository<T> : IRepository<T> where T : class
{
#region 数据上下文 /// <summary>
/// 数据上下文
/// </summary>
private ApplicationDbContext _Context; public Repository(ApplicationDbContext Context)
{
_Context = Context;
} #endregion #region 单模型 CRUD 操作 /// <summary>
/// 增加一条记录
/// </summary>
/// <param name="entity">实体模型</param>
/// <param name="IsCommit">是否提交(默认提交)</param>
/// <returns></returns>
public virtual bool Save(T entity,bool IsCommit=true)
{
_Context.Set<T>().Add(entity);
if (IsCommit)
return _Context.SaveChanges() > ;
else
return false;
}
/// <summary>
/// 增加一条记录(异步方式)
/// </summary>
/// <param name="entity">实体模型</param>
/// <param name="IsCommit">是否提交(默认提交)</param>
/// <returns></returns>
public virtual async Task<bool> SaveAsync(T entity, bool IsCommit = true)
{
_Context.Set<T>().Add(entity);
if (IsCommit)
return await Task.Run(() => _Context.SaveChanges() > );
else
return await Task.Run(() => false);
} /// <summary>
/// 更新一条记录
/// </summary>
/// <param name="entity">实体模型</param>
/// <param name="IsCommit">是否提交(默认提交)</param>
/// <returns></returns>
public virtual bool Update(T entity, bool IsCommit = true)
{
_Context.Set<T>().Attach(entity);
_Context.Entry<T>(entity).State = EntityState.Modified;
if (IsCommit)
return _Context.SaveChanges() > ;
else
return false;
}
/// <summary>
/// 更新一条记录(异步方式)
/// </summary>
/// <param name="entity">实体模型</param>
/// <param name="IsCommit">是否提交(默认提交)</param>
/// <returns></returns>
public virtual async Task<bool> UpdateAsync(T entity, bool IsCommit = true)
{
_Context.Set<T>().Attach(entity);
_Context.Entry<T>(entity).State = EntityState.Modified;
if (IsCommit)
return await Task.Run(() => _Context.SaveChanges() > );
else
return await Task.Run(() => false);
} /// <summary>
/// 增加或更新一条记录
/// </summary>
/// <param name="entity">实体模型</param>
/// <param name="IsSave">是否增加</param>
/// <param name="IsCommit">是否提交(默认提交)</param>
/// <returns></returns>
public virtual bool SaveOrUpdate(T entity, bool IsSave, bool IsCommit = true)
{
return IsSave ? Save(entity, IsCommit) : Update(entity, IsCommit);
}
/// <summary>
/// 增加或更新一条记录(异步方式)
/// </summary>
/// <param name="entity">实体模型</param>
/// <param name="IsSave">是否增加</param>
/// <param name="IsCommit">是否提交(默认提交)</param>
/// <returns></returns>
public virtual async Task<bool> SaveOrUpdateAsync(T entity, bool IsSave, bool IsCommit = true)
{
return IsSave ? await SaveAsync(entity, IsCommit) : await UpdateAsync(entity, IsCommit);
} /// <summary>
/// 通过Lamda表达式获取实体
/// </summary>
/// <param name="predicate">Lamda表达式(p=>p.Id==Id)</param>
/// <returns></returns>
public virtual T Get(Expression<Func<T, bool>> predicate)
{
return _Context.Set<T>().AsNoTracking().SingleOrDefault(predicate);
}
/// <summary>
/// 通过Lamda表达式获取实体(异步方式)
/// </summary>
/// <param name="predicate">Lamda表达式(p=>p.Id==Id)</param>
/// <returns></returns>
public virtual async Task<T> GetAsync(Expression<Func<T, bool>> predicate)
{
return await Task.Run(()=> _Context.Set<T>().AsNoTracking().SingleOrDefault(predicate));
} /// <summary>
/// 删除一条记录
/// </summary>
/// <param name="entity">实体模型</param>
/// <param name="IsCommit">是否提交(默认提交)</param>
/// <returns></returns>
public virtual bool Delete(T entity, bool IsCommit = true)
{
if (entity == null) return false;
_Context.Set<T>().Attach(entity);
_Context.Set<T>().Remove(entity); if (IsCommit)
return _Context.SaveChanges() > ;
else
return false;
}
/// <summary>
/// 删除一条记录(异步方式)
/// </summary>
/// <param name="entity">实体模型</param>
/// <param name="IsCommit">是否提交(默认提交)</param>
/// <returns></returns>
public virtual async Task<bool> DeleteAsync(T entity, bool IsCommit = true)
{
if (entity == null) return await Task.Run(() => false);
_Context.Set<T>().Attach(entity);
_Context.Set<T>().Remove(entity);
if (IsCommit)
return await Task.Run(() => _Context.SaveChanges() > );
else
return await Task.Run(() => false); ;
} #endregion #region 多模型 操作 /// <summary>
/// 增加多条记录,同一模型
/// </summary>
/// <param name="T1">实体模型集合</param>
/// <param name="IsCommit">是否提交(默认提交)</param>
/// <returns></returns>
public virtual bool SaveList(List<T> T1, bool IsCommit = true)
{
if (T1 == null || T1.Count == ) return false; T1.ToList().ForEach(item =>
{
_Context.Set<T>().Add(item);
}); if (IsCommit)
return _Context.SaveChanges() > ;
else
return false;
}
/// <summary>
/// 增加多条记录,同一模型(异步方式)
/// </summary>
/// <param name="T1">实体模型集合</param>
/// <param name="IsCommit">是否提交(默认提交)</param>
/// <returns></returns>
public virtual async Task<bool> SaveListAsync(List<T> T1, bool IsCommit = true)
{
if (T1 == null || T1.Count == ) return await Task.Run(() => false); T1.ToList().ForEach(item =>
{
_Context.Set<T>().Add(item);
}); if (IsCommit)
return await Task.Run(() => _Context.SaveChanges() > );
else
return await Task.Run(() => false);
} /// <summary>
/// 增加多条记录,独立模型
/// </summary>
/// <param name="T1">实体模型集合</param>
/// <param name="IsCommit">是否提交(默认提交)</param>
/// <returns></returns>
public virtual bool SaveList<T1>(List<T1> T, bool IsCommit = true) where T1 : class
{
if (T == null || T.Count == ) return false;
var tmp = _Context.ChangeTracker.Entries<T>().ToList();
foreach (var x in tmp)
{
var properties = typeof(T).GetTypeInfo().GetProperties();
foreach (var y in properties)
{
var entry = x.Property(y.Name);
entry.CurrentValue = entry.OriginalValue;
entry.IsModified = false;
y.SetValue(x.Entity, entry.OriginalValue);
}
x.State = EntityState.Unchanged;
}
T.ToList().ForEach(item =>
{
_Context.Set<T1>().Add(item);
});
if (IsCommit)
return _Context.SaveChanges() > ;
else
return false;
}
/// <summary>
/// 增加多条记录,独立模型(异步方式)
/// </summary>
/// <param name="T1">实体模型集合</param>
/// <param name="IsCommit">是否提交(默认提交)</param>
/// <returns></returns>
public virtual async Task<bool> SaveListAsync<T1>(List<T1> T, bool IsCommit = true) where T1 : class
{
if (T == null || T.Count == ) return await Task.Run(() => false);
var tmp = _Context.ChangeTracker.Entries<T>().ToList();
foreach (var x in tmp)
{
var properties = typeof(T).GetTypeInfo().GetProperties();
foreach (var y in properties)
{
var entry = x.Property(y.Name);
entry.CurrentValue = entry.OriginalValue;
entry.IsModified = false;
y.SetValue(x.Entity, entry.OriginalValue);
}
x.State = EntityState.Unchanged;
}
T.ToList().ForEach(item =>
{
_Context.Set<T1>().Add(item);
});
if (IsCommit)
return await Task.Run(() => _Context.SaveChanges() > );
else
return await Task.Run(() => false);
} /// <summary>
/// 更新多条记录,同一模型
/// </summary>
/// <param name="T1">实体模型集合</param>
/// <param name="IsCommit">是否提交(默认提交)</param>
/// <returns></returns>
public virtual bool UpdateList(List<T> T1, bool IsCommit = true)
{
if (T1 == null || T1.Count == ) return false; T1.ToList().ForEach(item =>
{
_Context.Set<T>().Attach(item);
_Context.Entry<T>(item).State = EntityState.Modified;
}); if (IsCommit)
return _Context.SaveChanges() > ;
else
return false;
}
/// <summary>
/// 更新多条记录,同一模型(异步方式)
/// </summary>
/// <param name="T1">实体模型集合</param>
/// <param name="IsCommit">是否提交(默认提交)</param>
/// <returns></returns>
public virtual async Task<bool> UpdateListAsync(List<T> T1, bool IsCommit = true)
{
if (T1 == null || T1.Count == ) return await Task.Run(() => false); T1.ToList().ForEach(item =>
{
_Context.Set<T>().Attach(item);
_Context.Entry<T>(item).State = EntityState.Modified;
}); if (IsCommit)
return await Task.Run(() => _Context.SaveChanges() > );
else
return await Task.Run(() => false);
} /// <summary>
/// 更新多条记录,独立模型
/// </summary>
/// <param name="T1">实体模型集合</param>
/// <param name="IsCommit">是否提交(默认提交)</param>
/// <returns></returns>
public virtual bool UpdateList<T1>(List<T1> T, bool IsCommit = true) where T1 : class
{
if (T == null || T.Count == ) return false; T.ToList().ForEach(item =>
{
_Context.Set<T1>().Attach(item);
_Context.Entry<T1>(item).State = EntityState.Modified;
}); if (IsCommit)
return _Context.SaveChanges() > ;
else
return false;
}
/// <summary>
/// 更新多条记录,独立模型(异步方式)
/// </summary>
/// <param name="T1">实体模型集合</param>
/// <param name="IsCommit">是否提交(默认提交)</param>
/// <returns></returns>
public virtual async Task<bool> UpdateListAsync<T1>(List<T1> T, bool IsCommit = true) where T1 : class
{
if (T == null || T.Count == ) return await Task.Run(() => false); T.ToList().ForEach(item =>
{
_Context.Set<T1>().Attach(item);
_Context.Entry<T1>(item).State = EntityState.Modified;
}); if (IsCommit)
return await Task.Run(() => _Context.SaveChanges() > );
else
return await Task.Run(() => false);
} /// <summary>
/// 删除多条记录,同一模型
/// </summary>
/// <param name="T1">实体模型集合</param>
/// <param name="IsCommit">是否提交(默认提交)</param>
/// <returns></returns>
public virtual bool DeleteList(List<T> T1, bool IsCommit = true)
{
if (T1 == null || T1.Count == ) return false; T1.ToList().ForEach(item =>
{
_Context.Set<T>().Attach(item);
_Context.Set<T>().Remove(item);
}); if (IsCommit)
return _Context.SaveChanges() > ;
else
return false;
}
/// <summary>
/// 删除多条记录,同一模型(异步方式)
/// </summary>
/// <param name="T1">实体模型集合</param>
/// <param name="IsCommit">是否提交(默认提交)</param>
/// <returns></returns>
public virtual async Task<bool> DeleteListAsync(List<T> T1, bool IsCommit = true)
{
if (T1 == null || T1.Count == ) return await Task.Run(() => false); T1.ToList().ForEach(item =>
{
_Context.Set<T>().Attach(item);
_Context.Set<T>().Remove(item);
}); if (IsCommit)
return await Task.Run(() => _Context.SaveChanges() > );
else
return await Task.Run(() => false);
} /// <summary>
/// 删除多条记录,独立模型
/// </summary>
/// <param name="T1">实体模型集合</param>
/// <param name="IsCommit">是否提交(默认提交)</param>
/// <returns></returns>
public virtual bool DeleteList<T1>(List<T1> T, bool IsCommit = true) where T1 : class
{
if (T == null || T.Count == ) return false; T.ToList().ForEach(item =>
{
_Context.Set<T1>().Attach(item);
_Context.Set<T1>().Remove(item);
}); if (IsCommit)
return _Context.SaveChanges() > ;
else
return false;
}
/// <summary>
/// 删除多条记录,独立模型(异步方式)
/// </summary>
/// <param name="T1">实体模型集合</param>
/// <param name="IsCommit">是否提交(默认提交)</param>
/// <returns></returns>
public virtual async Task<bool> DeleteListAsync<T1>(List<T1> T, bool IsCommit = true) where T1 : class
{
if (T == null || T.Count == ) return await Task.Run(() => false); T.ToList().ForEach(item =>
{
_Context.Set<T1>().Attach(item);
_Context.Set<T1>().Remove(item);
}); if (IsCommit)
return await Task.Run(() => _Context.SaveChanges() > );
else
return await Task.Run(() => false);
} /// <summary>
/// 通过Lamda表达式,删除一条或多条记录
/// </summary>
/// <param name="predicate"></param>
/// <param name="IsCommit"></param>
/// <returns></returns>
public virtual bool Delete(Expression<Func<T, bool>> predicate, bool IsCommit = true)
{
IQueryable<T> entry = (predicate == null) ? _Context.Set<T>().AsQueryable() : _Context.Set<T>().Where(predicate);
List<T> list = entry.ToList(); if (list != null && list.Count == ) return false;
list.ForEach(item => {
_Context.Set<T>().Attach(item);
_Context.Set<T>().Remove(item);
}); if (IsCommit)
return _Context.SaveChanges() > ;
else
return false;
}
/// <summary>
/// 通过Lamda表达式,删除一条或多条记录(异步方式)
/// </summary>
/// <param name="predicate"></param>
/// <param name="IsCommit"></param>
/// <returns></returns>
public virtual async Task<bool> DeleteAsync(Expression<Func<T, bool>> predicate, bool IsCommit = true)
{
IQueryable<T> entry = (predicate == null) ? _Context.Set<T>().AsQueryable() : _Context.Set<T>().Where(predicate);
List<T> list = entry.ToList(); if (list != null && list.Count == ) return await Task.Run(() => false);
list.ForEach(item => {
_Context.Set<T>().Attach(item);
_Context.Set<T>().Remove(item);
}); if (IsCommit)
return await Task.Run(() => _Context.SaveChanges() > );
else
return await Task.Run(() => false);
} #endregion #region 获取多条数据操作 /// <summary>
/// Lamda返回IQueryable集合,延时加载数据
/// </summary>
/// <param name="predicate"></param>
/// <returns></returns>
public virtual IQueryable<T> LoadAll(Expression<Func<T, bool>> predicate)
{
return predicate != null ? _Context.Set<T>().Where(predicate).AsNoTracking<T>() : _Context.Set<T>().AsQueryable<T>().AsNoTracking<T>();
}
/// <summary>
/// 返回IQueryable集合,延时加载数据(异步方式)
/// </summary>
/// <param name="predicate"></param>
/// <returns></returns>
public virtual async Task<IQueryable<T>> LoadAllAsync(Expression<Func<T, bool>> predicate)
{
return predicate != null ? await Task.Run(()=> _Context.Set<T>().Where(predicate).AsNoTracking<T>()) : await Task.Run(() => _Context.Set<T>().AsQueryable<T>().AsNoTracking<T>());
} /// <summary>
/// 返回List<T>集合,不采用延时加载
/// </summary>
/// <param name="predicate"></param>
/// <returns></returns>
public virtual List<T> LoadListAll(Expression<Func<T, bool>> predicate)
{
return predicate != null ? _Context.Set<T>().Where(predicate).AsNoTracking().ToList() : _Context.Set<T>().AsQueryable<T>().AsNoTracking().ToList();
}
// <summary>
/// 返回List<T>集合,不采用延时加载(异步方式)
/// </summary>
/// <param name="predicate"></param>
/// <returns></returns>
public virtual async Task<List<T>> LoadListAllAsync(Expression<Func<T, bool>> predicate)
{
return predicate != null ? await Task.Run(() => _Context.Set<T>().Where(predicate).AsNoTracking().ToList()) : await Task.Run(() => _Context.Set<T>().AsQueryable<T>().AsNoTracking().ToList());
} /// <summary>
/// T-Sql方式:返回IQueryable<T>集合
/// </summary>
/// <param name="sql">SQL语句</param>
/// <param name="para">Parameters参数</param>
/// <returns></returns>
public virtual IQueryable<T> LoadAllBySql(string sql, params DbParameter[] para)
{
return _Context.Set<T>().FromSql(sql, para);
}
/// <summary>
/// T-Sql方式:返回IQueryable<T>集合(异步方式)
/// </summary>
/// <param name="sql">SQL语句</param>
/// <param name="para">Parameters参数</param>
/// <returns></returns>
public virtual async Task<IQueryable<T>> LoadAllBySqlAsync(string sql, params DbParameter[] para)
{
return await Task.Run(() => _Context.Set<T>().FromSql(sql, para));
} /// <summary>
/// T-Sql方式:返回List<T>集合
/// </summary>
/// <param name="sql">SQL语句</param>
/// <param name="para">Parameters参数</param>
/// <returns></returns>
public virtual List<T> LoadListAllBySql(string sql, params DbParameter[] para)
{
return _Context.Set<T>().FromSql(sql, para).Cast<T>().ToList();
}
/// <summary>
/// T-Sql方式:返回List<T>集合(异步方式)
/// </summary>
/// <param name="sql">SQL语句</param>
/// <param name="para">Parameters参数</param>
/// <returns></returns>
public virtual async Task<List<T>> LoadListAllBySqlAsync(string sql, params DbParameter[] para)
{
return await Task.Run(()=> _Context.Set<T>().FromSql(sql, para).Cast<T>().ToList());
} /// <summary>
/// 可指定返回结果、排序、查询条件的通用查询方法,返回实体对象集合
/// </summary>
/// <typeparam name="TEntity">实体对象</typeparam>
/// <typeparam name="TOrderBy">排序字段类型</typeparam>
/// <typeparam name="TResult">数据结果,与TEntity一致</typeparam>
/// <param name="where">过滤条件,需要用到类型转换的需要提前处理与数据表一致的</param>
/// <param name="orderby">排序字段</param>
/// <param name="selector">返回结果(必须是模型中存在的字段)</param>
/// <param name="IsAsc">排序方向,true为正序false为倒序</param>
/// <returns>实体集合</returns>
public virtual List<TResult> QueryEntity<TEntity, TOrderBy, TResult>
(Expression<Func<TEntity, bool>> where,
Expression<Func<TEntity, TOrderBy>> orderby,
Expression<Func<TEntity, TResult>> selector,
bool IsAsc)
where TEntity : class
where TResult : class
{
IQueryable<TEntity> query = _Context.Set<TEntity>();
if (where != null)
{
query = query.Where(where);
} if (orderby != null)
{
query = IsAsc ? query.OrderBy(orderby) : query.OrderByDescending(orderby);
}
if (selector == null)
{
return query.Cast<TResult>().AsNoTracking().ToList();
}
return query.Select(selector).AsNoTracking().ToList();
}
/// <summary>
/// 可指定返回结果、排序、查询条件的通用查询方法,返回实体对象集合(异步方式)
/// </summary>
/// <typeparam name="TEntity">实体对象</typeparam>
/// <typeparam name="TOrderBy">排序字段类型</typeparam>
/// <typeparam name="TResult">数据结果,与TEntity一致</typeparam>
/// <param name="where">过滤条件,需要用到类型转换的需要提前处理与数据表一致的</param>
/// <param name="orderby">排序字段</param>
/// <param name="selector">返回结果(必须是模型中存在的字段)</param>
/// <param name="IsAsc">排序方向,true为正序false为倒序</param>
/// <returns>实体集合</returns>
public virtual async Task<List<TResult>> QueryEntityAsync<TEntity, TOrderBy, TResult>
(Expression<Func<TEntity, bool>> where,
Expression<Func<TEntity, TOrderBy>> orderby,
Expression<Func<TEntity, TResult>> selector,
bool IsAsc)
where TEntity : class
where TResult : class
{
IQueryable<TEntity> query = _Context.Set<TEntity>();
if (where != null)
{
query = query.Where(where);
} if (orderby != null)
{
query = IsAsc ? query.OrderBy(orderby) : query.OrderByDescending(orderby);
}
if (selector == null)
{
return await Task.Run(() => query.Cast<TResult>().AsNoTracking().ToList());
}
return await Task.Run(() => query.Select(selector).AsNoTracking().ToList());
} /// <summary>
/// 可指定返回结果、排序、查询条件的通用查询方法,返回Object对象集合
/// </summary>
/// <typeparam name="TEntity">实体对象</typeparam>
/// <typeparam name="TOrderBy">排序字段类型</typeparam>
/// <param name="where">过滤条件,需要用到类型转换的需要提前处理与数据表一致的</param>
/// <param name="orderby">排序字段</param>
/// <param name="selector">返回结果(必须是模型中存在的字段)</param>
/// <param name="IsAsc">排序方向,true为正序false为倒序</param>
/// <returns>自定义实体集合</returns>
public virtual List<object> QueryObject<TEntity, TOrderBy>
(Expression<Func<TEntity, bool>> where,
Expression<Func<TEntity, TOrderBy>> orderby,
Func<IQueryable<TEntity>,
List<object>> selector,
bool IsAsc)
where TEntity : class
{
IQueryable<TEntity> query = _Context.Set<TEntity>();
if (where != null)
{
query = query.Where(where);
} if (orderby != null)
{
query = IsAsc ? query.OrderBy(orderby) : query.OrderByDescending(orderby);
}
if (selector == null)
{
return query.AsNoTracking().ToList<object>();
}
return selector(query);
}
/// <summary>
/// 可指定返回结果、排序、查询条件的通用查询方法,返回Object对象集合(异步方式)
/// </summary>
/// <typeparam name="TEntity">实体对象</typeparam>
/// <typeparam name="TOrderBy">排序字段类型</typeparam>
/// <param name="where">过滤条件,需要用到类型转换的需要提前处理与数据表一致的</param>
/// <param name="orderby">排序字段</param>
/// <param name="selector">返回结果(必须是模型中存在的字段)</param>
/// <param name="IsAsc">排序方向,true为正序false为倒序</param>
/// <returns>自定义实体集合</returns>
public virtual async Task<List<object>> QueryObjectAsync<TEntity, TOrderBy>
(Expression<Func<TEntity, bool>> where,
Expression<Func<TEntity, TOrderBy>> orderby,
Func<IQueryable<TEntity>,
List<object>> selector,
bool IsAsc)
where TEntity : class
{
IQueryable<TEntity> query = _Context.Set<TEntity>();
if (where != null)
{
query = query.Where(where);
} if (orderby != null)
{
query = IsAsc ? query.OrderBy(orderby) : query.OrderByDescending(orderby);
}
if (selector == null)
{
return await Task.Run(() => query.AsNoTracking().ToList<object>());
}
return await Task.Run(() => selector(query));
} /// <summary>
/// 可指定返回结果、排序、查询条件的通用查询方法,返回动态类对象集合
/// </summary>
/// <typeparam name="TEntity">实体对象</typeparam>
/// <typeparam name="TOrderBy">排序字段类型</typeparam>
/// <param name="where">过滤条件,需要用到类型转换的需要提前处理与数据表一致的</param>
/// <param name="orderby">排序字段</param>
/// <param name="selector">返回结果(必须是模型中存在的字段)</param>
/// <param name="IsAsc">排序方向,true为正序false为倒序</param>
/// <returns>动态类</returns>
public virtual dynamic QueryDynamic<TEntity, TOrderBy>
(Expression<Func<TEntity, bool>> where,
Expression<Func<TEntity, TOrderBy>> orderby,
Func<IQueryable<TEntity>,
List<object>> selector,
bool IsAsc)
where TEntity : class
{
List<object> list = QueryObject<TEntity, TOrderBy>
(where, orderby, selector, IsAsc);
return Common.JsonHelper.JsonConvert.JsonClass(list);
}
/// <summary>
/// 可指定返回结果、排序、查询条件的通用查询方法,返回动态类对象集合(异步方式)
/// </summary>
/// <typeparam name="TEntity">实体对象</typeparam>
/// <typeparam name="TOrderBy">排序字段类型</typeparam>
/// <param name="where">过滤条件,需要用到类型转换的需要提前处理与数据表一致的</param>
/// <param name="orderby">排序字段</param>
/// <param name="selector">返回结果(必须是模型中存在的字段)</param>
/// <param name="IsAsc">排序方向,true为正序false为倒序</param>
/// <returns>动态类</returns>
public virtual async Task<dynamic> QueryDynamicAsync<TEntity, TOrderBy>
(Expression<Func<TEntity, bool>> where,
Expression<Func<TEntity, TOrderBy>> orderby,
Func<IQueryable<TEntity>,
List<object>> selector,
bool IsAsc)
where TEntity : class
{
List<object> list = QueryObject<TEntity, TOrderBy>
(where, orderby, selector, IsAsc);
return await Task.Run(() => Common.JsonHelper.JsonConvert.JsonClass(list)) ;
} #endregion #region 验证是否存在 /// <summary>
/// 验证当前条件是否存在相同项
/// </summary>
public virtual bool IsExist(Expression<Func<T, bool>> predicate)
{
var entry = _Context.Set<T>().Where(predicate);
return (entry.Any());
}
/// <summary>
/// 验证当前条件是否存在相同项(异步方式)
/// </summary>
public virtual async Task<bool> IsExistAsync(Expression<Func<T, bool>> predicate)
{
var entry = _Context.Set<T>().Where(predicate);
return await Task.Run(() => entry.Any());
} /// <summary>
/// 根据SQL验证实体对象是否存在
/// </summary>
public virtual bool IsExist(string sql, params DbParameter[] para)
{
return _Context.Database.ExecuteSqlCommand(sql, para) > ;
}
/// <summary>
/// 根据SQL验证实体对象是否存在(异步方式)
/// </summary>
public virtual async Task<bool> IsExistAsync(string sql, params DbParameter[] para)
{
return await Task.Run(() => _Context.Database.ExecuteSqlCommand(sql, para) > );
} #endregion }

希望跟大家一起学习Asp.net Core

刚开始接触,水平有限,很多东西都是自己的理解和翻阅网上大神的资料,如果有不对的地方和不理解的地方,希望大家指正!

虽然Asp.net Core 现在很火热,但是网上的很多资料都是前篇一律的复制,所以有很多问题我也暂时没有解决,希望大家能共同帮助一下!

原创文章 转载请尊重劳动成果 http://yuangang.cnblogs.com

上一篇:idea快捷键列表


下一篇:MVC+UnitOfWork+Repository+EF 之我见