mirror of
				https://github.com/nsnail/FreeSql.git
				synced 2025-11-04 01:05:27 +08:00 
			
		
		
		
	FreeSql.DbContext 融合 Repository + UnitOfWork
This commit is contained in:
		@@ -0,0 +1,25 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Text;
 | 
			
		||||
 | 
			
		||||
namespace FreeSql {
 | 
			
		||||
	internal class RepositoryDbContext<TEntity> : DbContext where TEntity : class {
 | 
			
		||||
 | 
			
		||||
		protected BaseRepository<TEntity> _repos;
 | 
			
		||||
		public RepositoryDbContext(IFreeSql orm, BaseRepository<TEntity> repos) : base() {
 | 
			
		||||
			_orm = orm;
 | 
			
		||||
			_repos = repos;
 | 
			
		||||
			_isUseUnitOfWork = false;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		public override object Set(Type entityType) {
 | 
			
		||||
			if (_dicSet.ContainsKey(entityType)) return _dicSet[entityType];
 | 
			
		||||
			var sd = Activator.CreateInstance(typeof(RepositoryDbSet<>).MakeGenericType(entityType), _repos);
 | 
			
		||||
			_dicSet.Add(entityType, sd);
 | 
			
		||||
			return sd;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		RepositoryDbSet<TEntity> _dbSet;
 | 
			
		||||
		public RepositoryDbSet<TEntity> DbSet => _dbSet ?? (_dbSet = Set<TEntity>() as RepositoryDbSet<TEntity>);
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user