/// <summary> /// 使用模块服务 /// </summary> /// <param name="provider">服务提供者</param> public override void UsePack(IServiceProvider provider) { using (IServiceScope scope = provider.CreateScope()) { TDbContext context = CreateDbContext(scope.ServiceProvider); if (context != null) { OSharpOptions options = scope.ServiceProvider.GetOSharpOptions(); OSharpDbContextOptions contextOptions = options.GetDbContextOptions(context.GetType()); if (contextOptions != null) { if (contextOptions.DatabaseType != DatabaseType.SqlServer) { throw new OsharpException($"上下文类型“{contextOptions.DatabaseType}”不是 SqlServer 类型"); } if (contextOptions.AutoMigrationEnabled) { context.CheckAndMigration(); int count = context.SaveChanges(); Debug.WriteLine($"Migration Save:{count}"); } } } } IsEnabled = true; }
/// <summary> /// 使用模块服务 /// </summary> /// <param name="provider">服务提供者</param> public override void UsePack(IServiceProvider provider) { using (IServiceScope scope = provider.CreateScope()) { TDbContext context = CreateDbContext(scope.ServiceProvider); if (context != null) { OSharpOptions options = scope.ServiceProvider.GetOSharpOptions(); OSharpDbContextOptions contextOptions = options.GetDbContextOptions(context.GetType()); if (contextOptions != null) { if (contextOptions.DatabaseType != DatabaseType.MySql) { throw new OsharpException($"上下文类型“{contextOptions.DatabaseType}”不是 {nameof(DatabaseType.MySql)} 类型"); } if (contextOptions.AutoMigrationEnabled) { context.CheckAndMigration(); } } } } IsEnabled = true; }
/// <summary> /// 应用模块服务 /// </summary> /// <param name="provider">服务提供者</param> public override void UsePack(IServiceProvider provider) { OSharpOptions options = provider.GetOSharpOptions(); using (IServiceScope scope = provider.CreateScope()) { ILogger logger = provider.GetService <ILoggerFactory>().CreateLogger(GetType()); TDbContext context = CreateDbContext(scope.ServiceProvider); if (context != null) { OSharpDbContextOptions contextOptions = options.GetDbContextOptions(context.GetType()); if (contextOptions == null) { logger.LogWarning($"上下文类型“{context.GetType()}”的数据库上下文配置不存在"); return; } if (contextOptions.DatabaseType != DatabaseType.SqlServer) { logger.LogWarning($"上下文类型“{contextOptions.DatabaseType}”不是 {nameof(DatabaseType.SqlServer)} 类型"); return; } if (contextOptions.AutoMigrationEnabled) { context.CheckAndMigration(); DbContextModelCache modelCache = scope.ServiceProvider.GetService <DbContextModelCache>(); if (modelCache != null) { modelCache.Set(context.GetType(), context.Model); } IsEnabled = true; } } } }
/// <summary> /// 获取指定数据上下文类型<typeparamref name="TEntity"/>的实例,并将同数据库连接字符串的上下文实例进行分组归类 /// </summary> /// <typeparam name="TEntity">实体类型</typeparam> /// <typeparam name="TKey">实体主键类型</typeparam> /// <returns><typeparamref name="TEntity"/>所属上下文类的实例</returns> public IDbContext GetDbContext <TEntity, TKey>() where TEntity : IEntity <TKey> where TKey : IEquatable <TKey> { IEntityConfigurationTypeFinder typeFinder = _serviceProvider.GetService <IEntityConfigurationTypeFinder>(); Type entityType = typeof(TEntity); Type dbContextType = typeFinder.GetDbContextTypeForEntity(entityType); OSharpDbContextOptions dbContextOptions = GetDbContextResolveOptions(dbContextType); DbContextResolveOptions resolveOptions = new DbContextResolveOptions(dbContextOptions); //已存在上下文对象,直接返回 DbContextBase dbContext = _dbContextMamager.Get(dbContextType, resolveOptions.ConnectionString); if (dbContext != null) { return(dbContext); } IDbContextResolver contextResolver = _serviceProvider.GetService <IDbContextResolver>(); dbContext = (DbContextBase)contextResolver.Resolve(resolveOptions); if (!dbContext.ExistsRelationalDatabase()) { throw new OsharpException($"数据上下文“{dbContext.GetType().FullName}”的数据库不存在,请通过 Migration 功能进行数据迁移创建数据库。"); } if (resolveOptions.ExistingConnection == null) { resolveOptions.ExistingConnection = dbContext.Database.GetDbConnection(); } _dbContextMamager.Add(dbContextOptions.ConnectionString, dbContext); return(dbContext); }
/// <summary> /// 获取指定实体所在的工作单元对象 /// </summary> /// <param name="entityType">实体类型</param> /// <returns>工作单元对象</returns> public IUnitOfWork GetUnitOfWork(Type entityType) { if (!entityType.IsEntityType()) { throw new OsharpException($"类型“{entityType}”不是实体类型"); } IUnitOfWork unitOfWork = _entityTypeUnitOfWorks.GetOrDefault(entityType); if (unitOfWork != null) { return(unitOfWork); } IEntityConfigurationTypeFinder typeFinder = _serviceProvider.GetService <IEntityConfigurationTypeFinder>(); Type dbContextType = typeFinder.GetDbContextTypeForEntity(entityType); if (dbContextType == null) { throw new OsharpException($"实体类“{entityType}”的所属上下文类型无法找到"); } OSharpDbContextOptions dbContextOptions = GetDbContextResolveOptions(dbContextType); DbContextResolveOptions resolveOptions = new DbContextResolveOptions(dbContextOptions); unitOfWork = _connStringUnitOfWorks.GetOrDefault(resolveOptions.ConnectionString); if (unitOfWork != null) { return(unitOfWork); } unitOfWork = ActivatorUtilities.CreateInstance <UnitOfWork>(_serviceProvider, resolveOptions); _entityTypeUnitOfWorks.TryAdd(entityType, unitOfWork); _connStringUnitOfWorks.TryAdd(resolveOptions.ConnectionString, unitOfWork); return(unitOfWork); }
/// <summary> /// 获取指定实体类型的Sql执行器 /// </summary> public static ISqlExecutor <TEntity, TKey> GetSqlExecutor <TEntity, TKey>(this IUnitOfWorkManager unitOfWorkManager) where TEntity : IEntity <TKey> { OSharpDbContextOptions options = unitOfWorkManager.GetDbContextResolveOptions(typeof(TEntity)); DatabaseType databaseType = options.DatabaseType; IList <ISqlExecutor <TEntity, TKey> > executors = unitOfWorkManager.ServiceProvider.GetServices <ISqlExecutor <TEntity, TKey> >().ToList(); return(executors.FirstOrDefault(m => m.DatabaseType == databaseType)); }
/// <summary> /// 初始化一个<see cref="DbContextBase"/>类型的新实例 /// </summary> protected DbContextBase(DbContextOptions options, IEntityConfigurationTypeFinder typeFinder) : base(options) { _typeFinder = typeFinder; IOptions <OSharpOptions> osharpOptions = this.GetService <IOptions <OSharpOptions> >(); _osharpDbOptions = osharpOptions?.Value.DbContexts.Values.FirstOrDefault(m => m.DbContextType == GetType()); _logger = this.GetService <ILoggerFactory>().CreateLogger(GetType()); }
private OSharpDbContextOptions GetDbContextResolveOptions(Type dbContextType) { OSharpDbContextOptions dbContextOptions = _serviceProvider.GetOSharpOptions()?.GetDbContextOptions(dbContextType); if (dbContextOptions == null) { throw new OsharpException($"无法找到数据上下文“{dbContextType}”的配置信息"); } return(dbContextOptions); }
/// <summary> /// 获取指定实体类型的数据上下文选项 /// </summary> public static OSharpDbContextOptions GetDbContextResolveOptions(this IUnitOfWorkManager unitOfWorkManager, Type entityType) { Type dbContextType = unitOfWorkManager.GetDbContextType(entityType); OSharpDbContextOptions dbContextOptions = unitOfWorkManager.ServiceProvider.GetOSharpOptions()?.GetDbContextOptions(dbContextType); if (dbContextOptions == null) { throw new OsharpException($"无法找到数据上下文“{dbContextType}”的配置信息"); } return(dbContextOptions); }
/// <summary> /// 初始化一个<see cref="DbContextBase{TDbContext}"/>类型的新实例 /// </summary> protected DbContextBase(DbContextOptions options, IEntityConfigurationTypeFinder typeFinder) : base(options) { _typeFinder = typeFinder; IOptionsMonitor <OSharpOptions> osharpOptions = ServiceLocator.Instance.GetService <IOptionsMonitor <OSharpOptions> >(); if (osharpOptions != null) { _osharpDbOptions = osharpOptions.CurrentValue.DbContextOptionses.Values.FirstOrDefault(m => m.DbContextType == typeof(TDbContext)); } }
/// <summary> /// 初始化一个<see cref="DbContextBase"/>类型的新实例 /// </summary> protected DbContextBase(DbContextOptions options, IEntityConfigurationTypeFinder typeFinder) : base(options) { _typeFinder = typeFinder; if (ServiceLocator.Instance.IsProviderEnabled) { IOptions <OSharpOptions> osharpOptions = ServiceLocator.Instance.GetService <IOptions <OSharpOptions> >(); _osharpDbOptions = osharpOptions?.Value.DbContextOptionses.Values.FirstOrDefault(m => m.DbContextType == GetType()); _logger = ServiceLocator.Instance.GetLogger(GetType()); } }
private OSharpDbContextOptions GetDbContextResolveOptions(Type dbContextType) { IOptionsMonitor <OSharpOptions> osharpOptions = _serviceProvider.GetService <IOptionsMonitor <OSharpOptions> >(); OSharpDbContextOptions dbContextOptions = osharpOptions.CurrentValue.DbContextOptionses.Values.SingleOrDefault(m => m.DbContextType == dbContextType); if (dbContextOptions == null) { throw new OsharpException($"无法找到数据上下文“{dbContextType}”的配置信息"); } return(dbContextOptions); }
/// <summary> /// 获取指定数据上下文类型<typeparamref name="TEntity"/>的实例 /// </summary> /// <typeparam name="TEntity">实体类型</typeparam> /// <typeparam name="TKey">实体主键类型</typeparam> /// <returns><typeparamref name="TEntity"/>所属上下文类的实例</returns> public IDbContext GetDbContext <TEntity, TKey>() where TEntity : IEntity <TKey> where TKey : IEquatable <TKey> { IEntityConfigurationTypeFinder typeFinder = _serviceProvider.GetService <IEntityConfigurationTypeFinder>(); Type entityType = typeof(TEntity); Type dbContextType = typeFinder.GetDbContextTypeForEntity(entityType); DbContext dbContext; OSharpDbContextOptions dbContextOptions = GetDbContextResolveOptions(dbContextType); DbContextResolveOptions resolveOptions = new DbContextResolveOptions(dbContextOptions); IDbContextResolver contextResolver = _serviceProvider.GetService <IDbContextResolver>(); ActiveTransactionInfo transInfo = ActiveTransactionInfos.GetOrDefault(resolveOptions.ConnectionString); //连接字符串的事务不存在,添加起始上下文事务信息 if (transInfo == null) { resolveOptions.ExistingConnection = null; dbContext = contextResolver.Resolve(resolveOptions); RelationalDatabaseCreator dbCreator; if ((dbCreator = dbContext.GetService <IDatabaseCreator>() as RelationalDatabaseCreator) != null) { if (!dbCreator.Exists()) { throw new OsharpException($"数据上下文“{dbContext.GetType().FullName}”的数据库不存在,请通过 Migration 功能进行数据迁移创建数据库。"); } } IDbContextTransaction transaction = dbContext.Database.BeginTransaction(); transInfo = new ActiveTransactionInfo(transaction, dbContext); ActiveTransactionInfos[resolveOptions.ConnectionString] = transInfo; } else { resolveOptions.ExistingConnection = transInfo.DbContextTransaction.GetDbTransaction().Connection; //相同连接串相同上下文类型并且已存在对象,直接返回上下文对象 if (transInfo.StarterDbContext.GetType() == resolveOptions.DbContextType) { return(transInfo.StarterDbContext as IDbContext); } dbContext = contextResolver.Resolve(resolveOptions); if (dbContext.IsRelationalTransaction()) { dbContext.Database.UseTransaction(transInfo.DbContextTransaction.GetDbTransaction()); } else { dbContext.Database.BeginTransaction(); } transInfo.AttendedDbContexts.Add(dbContext); } return(dbContext as IDbContext); }
public override string GetConnectionString() { if (_serviceProvider == null) { string str = AppSettingsManager.Get("OSharp:DbContexts:MySql:ConnectionString"); return(str); } OSharpOptions options = _serviceProvider.GetOSharpOptions(); OSharpDbContextOptions contextOptions = options.GetDbContextOptions(typeof(DefaultDbContext)); if (contextOptions == null) { throw new OsharpException($"上下文“{typeof(DefaultDbContext)}”的配置信息不存在"); } return(contextOptions.ConnectionString); }
/// <summary> /// 应用模块服务 /// </summary> /// <param name="provider">服务提供者</param> public override void UsePack(IServiceProvider provider) { OSharpOptions options = provider.GetOSharpOptions(); OSharpDbContextOptions contextOptions = options.GetDbContextOptions(typeof(TDbContext)); if (contextOptions?.DatabaseType != DatabaseType) { return; } using (IServiceScope scope = provider.CreateScope()) { TDbContext context = CreateDbContext(scope.ServiceProvider); if (context != null && contextOptions.AutoMigrationEnabled) { context.CheckAndMigration(); DbContextModelCache modelCache = scope.ServiceProvider.GetService <DbContextModelCache>(); modelCache?.Set(context.GetType(), context.Model); } } IsEnabled = true; }
/// <summary> /// 初始化一个<see cref="DbContextResolveOptions"/>类型的新实例 /// </summary> public DbContextResolveOptions(OSharpDbContextOptions options) { DbContextType = options.DbContextType; ConnectionString = options.ConnectionString; DatabaseType = options.DatabaseType; }