设为首页 加入收藏

TOP

IOC+EF+Core项目搭建EF封装(一)(三)
2019-10-09 20:03:18 】 浏览:228
Tags:IOC Core 项目 搭建 封装
entity">
Entity</param> public virtual void Update(TEntity entity) { if (entity == null) throw new ArgumentNullException(nameof(entity)); try { Entities.Update(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 Update(IEnumerable<TEntity> entities) { if (entities == null) throw new ArgumentNullException(nameof(entities)); try { Entities.UpdateRange(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="entity">Entity</param> public virtual void Delete(TEntity entity) { if (entity == null) throw new ArgumentNullException(nameof(entity)); try { Entities.Remove(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 Delete(IEnumerable<TEntity> entities) { if (entities == null) throw new ArgumentNullException(nameof(entities)); try { Entities.RemoveRange(entities); _context.SaveChanges(); } catch (DbUpdateException exception) { //ensure that the detailed error text is saved in the Log throw new Exception(GetFullErrorTextAndRollbackEntityChanges(exception), exception); } } #endregion #region 属性 /// <summary> /// 获取表 /// </summary> public virtual IQueryable<TEntity> Table => Entities; /// <summary> /// 获取一个启用“no tracking”(EF特性)的表,仅当您仅为只读操作加载记录时才使用它 /// </summary> public virtual IQueryable<TEntity> TableNoTracking => Entities.AsNoTracking(); /// <summary> /// 获取设置模板 /// </summary> protected virtual DbSet<TEntity> Entities { get { if (_entities == null) _entities = _context.Set<TEntity>(); return _entities; } } #endregion }

ef的模型映射封装

 /// <summary>
    /// 表示数据库上下文模型映射配置
    /// </summary>
    public partial interface IMappingConfiguration
    {
        /// <summary>
        /// 应用此映射配置
        /// </summary>
        /// <param name="modelBuilder">用于构造数据库上下文模型的生成器</param>
        void ApplyConfiguration(ModelBuilder modelBuilder);
    }
/// <summary>
    /// 表示基本实体映射配置
    /// </summary>
    /// <typeparam name="TEntity">Entity type</typeparam>
    public partial class NopEntityTypeConfiguration<TEntity> : IMappingConfiguration, IEntityTypeConfiguration<TEntity> where TEntity : BaseEntity
    {
        #region Utilities
        /// <summary>
        /// Develo
首页 上一页 1 2 3 4 5 下一页 尾页 3/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇关于部署版本遇到的---警告: 程序.. 下一篇随机数生成

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目