设为首页 加入收藏

TOP

IOC+EF+Core项目搭建EF封装(一)(二)
2019-10-09 20:03:18 】 浏览:230
Tags:IOC Core 项目 搭建 封装
mmary>
/// 添加 /// </summary> void Insert(TEntity entity); /// <summary> /// 批量添加 /// </summary> void Insert(IEnumerable<TEntity> entities); /// <summary> /// 修改 /// </summary> void Update(TEntity entity); /// <summary> /// 批量修改 /// </summary> void Update(IEnumerable<TEntity> entities); /// <summary> /// 删除 /// </summary> void Delete(TEntity entity); /// <summary> /// 批量删除 /// </summary> void Delete(IEnumerable<TEntity> entities); #endregion #region 属性 /// <summary> /// 查询数据集 /// </summary> IQueryable<TEntity> Table { get; } /// <summary> /// 获取一个启用“no tracking”(EF特性)的表,仅当您仅为只读操作加载记录时才使用它 /// </summary> IQueryable<TEntity> TableNoTracking { get; } #endregion }
/// <summary>
    /// 基础仓储实现
    /// </summary>
    public partial class EfRepository<TEntity> : IRepository<TEntity> where TEntity : BaseEntity
    {
        #region 参数
        private readonly IDbContext _context;
        private DbSet<TEntity> _entities;
        #endregion

        #region 构造函数
        public EfRepository(IDbContext context)
        {
            this._context = context;
        }
        #endregion

        #region 公共方法
        /// <summary>
        /// 实体更改的回滚并返回完整的错误消息
        /// </summary>
        /// <param name="exception">Exception</param>
        /// <returns>Error message</returns>
        protected string GetFullErrorTextAndRollbackEntityChanges(DbUpdateException exception)
        {
            //回滚实体
            if (_context is DbContext dbContext)
            {
                var entries = dbContext.ChangeTracker.Entries()
                    .Where(e => e.State == EntityState.Added || e.State == EntityState.Modified).ToList();

                entries.ForEach(entry => entry.State = EntityState.Unchanged);
            }
            _context.SaveChanges();
            return exception.ToString();
        }
        #endregion

        #region 方法
        /// <summary>
        /// 按id获取实体
        /// </summary>
        /// <param name="id">Identifier</param>
        /// <returns>Entity</returns>
        public virtual TEntity GetById(object id)
        {
            return Entities.Find(id);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity">Entity</param>
        public virtual void Insert(TEntity entity)
        {
            if (entity == null)
                throw new ArgumentNullException(nameof(entity));

            try
            {
                Entities.Add(entity);
                _context.SaveChanges();
            }
            catch (DbUpdateException exception)
            {
                //ensure that the detailed error text is saved in the Log
                throw new Exception(GetFullErrorTextAndRollbackEntityChanges(exception), exception);
            }
        }

        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="entities">Entities</param>
        public virtual void Insert(IEnumerable<TEntity> entities)
        {
            if (entities == null)
                throw new ArgumentNullException(nameof(entities));

            try
            {
                Entities.AddRange(entities);
                _context.SaveChanges();
            }
            catch (DbUpdateException exception)
            {
                //ensure that the detailed error text is saved in the Log
                throw new Exception(GetFullErrorTextAndRollbackEntityChanges(exception), exception);
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="
首页 上一页 1 2 3 4 5 下一页 尾页 2/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇关于部署版本遇到的---警告: 程序.. 下一篇随机数生成

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目