目录索引 

【无私分享: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

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

  1. 【无私分享:ASP.NET CORE 项目实战(第九章)】创建区域Areas,添加TagHelper

    目录索引 [无私分享:ASP.NET CORE 项目实战]目录索引 简介 在Asp.net Core VS2015中,我们发现还有很多不太简便的地方,比如右击添加视图,转到试图页等功能图不见了,虽然我 ...

  2. 【无私分享:ASP.NET CORE 项目实战(第二章)】添加EF上下文对象,添加接口、实现类以及无处不在的依赖注入(DI)

    目录索引 [无私分享:ASP.NET CORE 项目实战]目录索引 简介 上一章,我们介绍了安装和新建控制器.视图,这一章我们来创建个数据模型,并且添加接口和实现类. 添加EF上下文对象 按照我们以前 ...

  3. [转]【无私分享:ASP.NET CORE 项目实战(第九章)】创建区域Areas,添加TagHelper

    本文转自:http://www.cnblogs.com/zhangxiaolei521/p/5808417.html 目录索引 [无私分享:ASP.NET CORE 项目实战]目录索引 简介 在Asp ...

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

    简介 首先,我们的  [无私分享:从入门到精通ASP.NET MVC]   系列已经接近尾声,希望大家在这个过程中学到了一些思路和方法,而不仅仅是源码. 因为是第一次写博客,我感觉还是比较混乱的,其中 ...

  5. 【无私分享:ASP.NET CORE 项目实战(第十四章)】图形验证码的实现

    目录索引 [无私分享:ASP.NET CORE 项目实战]目录索引 简介 很长时间没有来更新博客了,一是,最近有些忙,二是,Core也是一直在摸索中,其实已经完成了一个框架了,并且正在准备在生产环境中 ...

  6. 【无私分享:ASP.NET CORE 项目实战(第十三章)】Asp.net Core 使用MyCat分布式数据库,实现读写分离

    目录索引 [无私分享:ASP.NET CORE 项目实战]目录索引 简介 MyCat2.0版本很快就发布了,关于MyCat的动态和一些问题,大家可以加一下MyCat的官方QQ群:106088787.我 ...

  7. 【无私分享:ASP.NET CORE 项目实战(第十二章)】添加对SqlServer、MySql、Oracle的支持

    目录索引 [无私分享:ASP.NET CORE 项目实战]目录索引 简介 增加对多数据库的支持,并不是意味着同时对多种数据库操作,当然,后面,我们会尝试同时对多种数据库操作,这可能需要多个上下文,暂且 ...

  8. 【无私分享:ASP.NET CORE 项目实战(第十一章)】Asp.net Core 缓存 MemoryCache 和 Redis

    目录索引 [无私分享:ASP.NET CORE 项目实战]目录索引 简介 经过 N 久反复的尝试,翻阅了网上无数的资料,GitHub上下载了十几个源码参考, Memory 和 Redis 终于写出一个 ...

  9. 【无私分享:ASP.NET CORE 项目实战(第十章)】发布项目到 Linux 上运行 Core 项目

    目录索引 [无私分享:ASP.NET CORE 项目实战]目录索引 简介 ASP.Net Core 给我们带来的最大的亮点就是跨平台,我在我电脑(win7)上用虚拟机建了个 CentOS7 ,来演示下 ...

  10. 【无私分享:ASP.NET CORE 项目实战(第八章)】读取配置文件(二) 读取自定义配置文件

    目录索引 [无私分享:ASP.NET CORE 项目实战]目录索引 简介 我们在 读取配置文件(一) appsettings.json 中介绍了,如何读取appsettings.json. 但随之产生 ...

随机推荐

  1. Gradle 实现 Android 多渠道定制化打包

    Gradle 实现 Android 多渠道定制化打包 版权声明:本文为博主原创文章,未经博主允许不得转载. 最近在项目中遇到需要实现 Apk 多渠道.定制化打包, Google .百度查找了一些资料, ...

  2. 掌握javascript中的最基础数据结构-----数组

    这是一篇<数据结构与算法javascript描述>的读书笔记.主要梳理了关于数组的知识.部分内容及源码来自原作. 书中第一章介绍了如何配置javascript运行环境:javascript ...

  3. Log4net - 项目使用的一个简单Demo

    参考页面: http://www.yuanjiaocheng.net/entity/entitytypes.html http://www.yuanjiaocheng.net/entity/entit ...

  4. [数据结构]——堆(Heap)、堆排序和TopK

    堆(heap),是一种特殊的数据结构.之所以特殊,因为堆的形象化是一个棵完全二叉树,并且满足任意节点始终不大于(或者不小于)左右子节点(有别于二叉搜索树Binary Search Tree).其中,前 ...

  5. 简单分析JavaScript中的面向对象

    初学JavaScript的时候有人会认为JavaScript不是一门面向对象的语言,因为JS是没有类的概念的,但是这并不代表JavaScript没有对象的存在,而且JavaScript也提供了其它的方 ...

  6. docker – 你应该知道的10件事

      容器并不是一个全新的技术,但这并不妨碍Docker如风暴一样席卷整个世界. 如果你在IT圈里,你一定听说过Docker.就算与其他热门技术,如:Puppet/Chef,Hadoop或者MongoD ...

  7. 一条Sql语句分组排序并且限制显示的数据条数

    如果我想得到这样一个结果集:分组排序,并且每组限定记录集的数量,用一条SQL语句能办到吗? 比如说,我想找出学生期末考试中,每科的前3名,并按成绩排序,只用一条SQL语句,该怎么写? 表[TScore ...

  8. postgresql无法安装pldbgapi的问题

    要对函数进行调试需要安装插件pldbgapi,当初在windows上面的postgresql实例中执行了一下语句就安装上了: create extension pldbgapi; 但是在linux中执 ...

  9. Web前端需要熟悉大学里【高大上】的计算机专业课吗?

    作为一名刚刚大学毕业,进入新的学习阶段的研究生,我必须说大学的专业课非常重要!不管你信不信,事实就是如此! 一.大学学习的专业课非常重要,它决定了我们能走到什么高度 前端的发展非常快,我常常觉得刚刚关 ...

  10. 【python之路3】if 语句

    1.if语句用法(if....else....) #!/usr/bin/env python # -*- coding:utf-8 -*- my_name = raw_input("plea ...