目录索引 

【无私分享: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. Html 制作相册

    本文主要讲述采用Html5+jQuery+CSS 制作相册的小小记录. 主要功能点: Html5进行布局 调用jQuery(借用官网的一句话:The Write Less, Do More)极大的简化 ...

  2. Android Butterknife 8.4.0 使用方法总结

    转载请标明出处:http://www.cnblogs.com/zhaoyanjun/p/6016341.html 本文出自[赵彦军的博客] 前言 ButterKnife 简介 ButterKnife是 ...

  3. asp.net core 实战之 redis 负载均衡和"高可用"实现

    1.概述 分布式系统缓存已经变得不可或缺,本文主要阐述如何实现redis主从复制集群的负载均衡,以及 redis的"高可用"实现, 呵呵双引号的"高可用"并不是 ...

  4. MySQL: Fabric 搭建 HA

    搭建好Fabric之后,就可以在它的基础上创建HA Group. Shard Group.HA+Shard Group等.这里来说明一下如何快速的搭建HA环境. Fabric 192.168.2.23 ...

  5. RMS:Microsoft Office检测到您的信息权限管理配置有问题。有关详细信息,请与管理员联系。(转)

    原文:https://zhidao.baidu.com/question/435088233.html RMS有两种方式: 1.使用微软的服务器,这个是连接到微软的服务器上面做权限控制,在今年5月份之 ...

  6. python之浅拷贝和深拷贝

    1.浅拷贝 1>赋值:从下面的例子我们可以看到赋值之后新变量的内存地址并没有发生任何变化,实际上python中的赋值操作不会开辟新的内存空间,它只是复制了新对象的引用,也就是说除了b这个名字以外 ...

  7. SqlHelper中IN集合场景下的参数处理

    我手头有个古老的项目,持久层用的是古老的ADO.net.前两天去昆明旅游,其中的一个景点是云南民族村,通过导游介绍知道了一个古老的民族——基诺族,“基”在这个族内代表舅舅,“基诺”意为“跟在舅舅后边” ...

  8. 提高(Android)开发效率的工具与网站

    Dev_Tools 写这篇不是为了"水",是因为有些工具确实不错,但是换个电脑要找到或者在某个时候你想用但是你只知道存在了书签里.然后就只能下载chrome然后登录账户同步书签了. ...

  9. MVC中的AOP

    一. AOP是什么? AOP(Aspect-Oriented Programming),面向切面编程,看着是跟OOP(面向对象编程)挺相近的,但实际上又有什么区别呢?OOP具有封装,继承,多态等东西来 ...

  10. WCF学习之旅—WCF中传统的异常处理(十六)

    WCF中的异常处理 在软件开发过程中,不可能没有异常的出现,所以在开发过程中,对不可预知的异常进行解决时,异常处理显得尤为重要.对于一般的.NET系统来说,我们简单地借助try/catch可以很容易地 ...