mirror of
				https://github.com/nsnail/FreeSql.git
				synced 2025-11-04 17:20:49 +08:00 
			
		
		
		
	## v0.11.18
This commit is contained in:
		@@ -92,6 +92,7 @@ namespace FreeSql
 | 
			
		||||
 | 
			
		||||
        protected List<IDbSet> _listSet = new List<IDbSet>();
 | 
			
		||||
        protected Dictionary<Type, IDbSet> _dicSet = new Dictionary<Type, IDbSet>();
 | 
			
		||||
        internal Dictionary<Type, IDbSet> InternalDicSet => _dicSet;
 | 
			
		||||
        public DbSet<TEntity> Set<TEntity>() where TEntity : class => this.Set(typeof(TEntity)) as DbSet<TEntity>;
 | 
			
		||||
        public virtual IDbSet Set(Type entityType)
 | 
			
		||||
        {
 | 
			
		||||
@@ -105,12 +106,21 @@ namespace FreeSql
 | 
			
		||||
        #endregion
 | 
			
		||||
 | 
			
		||||
        #region DbSet 快速代理
 | 
			
		||||
        void CheckEntityTypeOrThrow(Type entityType)
 | 
			
		||||
        {
 | 
			
		||||
            if (Orm.CodeFirst.GetTableByEntity(entityType) == null)
 | 
			
		||||
                throw new ArgumentException($"参数 data 类型错误 {entityType.FullName} ");
 | 
			
		||||
        }
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// 添加
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <typeparam name="TEntity"></typeparam>
 | 
			
		||||
        /// <param name="data"></param>
 | 
			
		||||
        public void Add<TEntity>(TEntity data) where TEntity : class => this.Set<TEntity>().Add(data);
 | 
			
		||||
        public void Add<TEntity>(TEntity data) where TEntity : class
 | 
			
		||||
        {
 | 
			
		||||
            CheckEntityTypeOrThrow(typeof(TEntity));
 | 
			
		||||
            this.Set<TEntity>().Add(data);
 | 
			
		||||
        }
 | 
			
		||||
        public void AddRange<TEntity>(IEnumerable<TEntity> data) where TEntity : class => this.Set<TEntity>().AddRange(data);
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
@@ -118,7 +128,11 @@ namespace FreeSql
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <typeparam name="TEntity"></typeparam>
 | 
			
		||||
        /// <param name="data"></param>
 | 
			
		||||
        public void Update<TEntity>(TEntity data) where TEntity : class => this.Set<TEntity>().Update(data);
 | 
			
		||||
        public void Update<TEntity>(TEntity data) where TEntity : class
 | 
			
		||||
        {
 | 
			
		||||
            CheckEntityTypeOrThrow(typeof(TEntity));
 | 
			
		||||
            this.Set<TEntity>().Update(data);
 | 
			
		||||
        }
 | 
			
		||||
        public void UpdateRange<TEntity>(IEnumerable<TEntity> data) where TEntity : class => this.Set<TEntity>().UpdateRange(data);
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
@@ -126,7 +140,11 @@ namespace FreeSql
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <typeparam name="TEntity"></typeparam>
 | 
			
		||||
        /// <param name="data"></param>
 | 
			
		||||
        public void Remove<TEntity>(TEntity data) where TEntity : class => this.Set<TEntity>().Remove(data);
 | 
			
		||||
        public void Remove<TEntity>(TEntity data) where TEntity : class
 | 
			
		||||
        {
 | 
			
		||||
            CheckEntityTypeOrThrow(typeof(TEntity));
 | 
			
		||||
            this.Set<TEntity>().Remove(data);
 | 
			
		||||
        }
 | 
			
		||||
        public void RemoveRange<TEntity>(IEnumerable<TEntity> data) where TEntity : class => this.Set<TEntity>().RemoveRange(data);
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
@@ -134,21 +152,32 @@ namespace FreeSql
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <typeparam name="TEntity"></typeparam>
 | 
			
		||||
        /// <param name="data"></param>
 | 
			
		||||
        public void AddOrUpdate<TEntity>(TEntity data) where TEntity : class => this.Set<TEntity>().AddOrUpdate(data);
 | 
			
		||||
 | 
			
		||||
        public void AddOrUpdate<TEntity>(TEntity data) where TEntity : class
 | 
			
		||||
        {
 | 
			
		||||
            CheckEntityTypeOrThrow(typeof(TEntity));
 | 
			
		||||
            this.Set<TEntity>().AddOrUpdate(data);
 | 
			
		||||
        }
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// 保存实体的指定 ManyToMany 导航属性
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <param name="data">实体对象</param>
 | 
			
		||||
        /// <param name="propertyName">属性名</param>
 | 
			
		||||
        public void SaveManyToMany<TEntity>(TEntity data, string propertyName) where TEntity : class => this.Set<TEntity>().SaveManyToMany(data, propertyName);
 | 
			
		||||
        public void SaveManyToMany<TEntity>(TEntity data, string propertyName) where TEntity : class
 | 
			
		||||
        {
 | 
			
		||||
            CheckEntityTypeOrThrow(typeof(TEntity));
 | 
			
		||||
            this.Set<TEntity>().SaveManyToMany(data, propertyName);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// 附加实体,可用于不查询就更新或删除
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <typeparam name="TEntity"></typeparam>
 | 
			
		||||
        /// <param name="data"></param>
 | 
			
		||||
        public void Attach<TEntity>(TEntity data) where TEntity : class => this.Set<TEntity>().Attach(data);
 | 
			
		||||
        public void Attach<TEntity>(TEntity data) where TEntity : class
 | 
			
		||||
        {
 | 
			
		||||
            CheckEntityTypeOrThrow(typeof(TEntity));
 | 
			
		||||
            this.Set<TEntity>().Attach(data);
 | 
			
		||||
        }
 | 
			
		||||
        public void AttachRange<TEntity>(IEnumerable<TEntity> data) where TEntity : class => this.Set<TEntity>().AttachRange(data);
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
@@ -158,19 +187,36 @@ namespace FreeSql
 | 
			
		||||
        /// <param name="data"></param>
 | 
			
		||||
        public DbContext AttachOnlyPrimary<TEntity>(TEntity data) where TEntity : class
 | 
			
		||||
        {
 | 
			
		||||
            CheckEntityTypeOrThrow(typeof(TEntity));
 | 
			
		||||
            this.Set<TEntity>().AttachOnlyPrimary(data);
 | 
			
		||||
            return this;
 | 
			
		||||
        }
 | 
			
		||||
#if net40
 | 
			
		||||
#else
 | 
			
		||||
        public Task AddAsync<TEntity>(TEntity data) where TEntity : class => this.Set<TEntity>().AddAsync(data);
 | 
			
		||||
        public Task AddAsync<TEntity>(TEntity data) where TEntity : class
 | 
			
		||||
        {
 | 
			
		||||
            CheckEntityTypeOrThrow(typeof(TEntity));
 | 
			
		||||
            return this.Set<TEntity>().AddAsync(data);
 | 
			
		||||
        }
 | 
			
		||||
        public Task AddRangeAsync<TEntity>(IEnumerable<TEntity> data) where TEntity : class => this.Set<TEntity>().AddRangeAsync(data);
 | 
			
		||||
 | 
			
		||||
        public Task UpdateAsync<TEntity>(TEntity data) where TEntity : class => this.Set<TEntity>().UpdateAsync(data);
 | 
			
		||||
        public Task UpdateAsync<TEntity>(TEntity data) where TEntity : class
 | 
			
		||||
        {
 | 
			
		||||
            CheckEntityTypeOrThrow(typeof(TEntity));
 | 
			
		||||
            return this.Set<TEntity>().UpdateAsync(data);
 | 
			
		||||
        }
 | 
			
		||||
        public Task UpdateRangeAsync<TEntity>(IEnumerable<TEntity> data) where TEntity : class => this.Set<TEntity>().UpdateRangeAsync(data);
 | 
			
		||||
 | 
			
		||||
        public Task AddOrUpdateAsync<TEntity>(TEntity data) where TEntity : class => this.Set<TEntity>().AddOrUpdateAsync(data);
 | 
			
		||||
        public Task SaveManyToManyAsync<TEntity>(TEntity data, string propertyName) where TEntity : class => this.Set<TEntity>().SaveManyToManyAsync(data, propertyName);
 | 
			
		||||
        public Task AddOrUpdateAsync<TEntity>(TEntity data) where TEntity : class
 | 
			
		||||
        {
 | 
			
		||||
            CheckEntityTypeOrThrow(typeof(TEntity));
 | 
			
		||||
            return this.Set<TEntity>().AddOrUpdateAsync(data);
 | 
			
		||||
        }
 | 
			
		||||
        public Task SaveManyToManyAsync<TEntity>(TEntity data, string propertyName) where TEntity : class
 | 
			
		||||
        {
 | 
			
		||||
            CheckEntityTypeOrThrow(typeof(TEntity));
 | 
			
		||||
            return this.Set<TEntity>().SaveManyToManyAsync(data, propertyName);
 | 
			
		||||
        }
 | 
			
		||||
#endif
 | 
			
		||||
        #endregion
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user