/// <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) { 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> /// 使用模块服务 /// </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; }
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; }