public static String GetConnectionString(this DbOptions options, Type dbContextType) { var settings = options.DbContextSettings.GetOrDefault(dbContextType, () => DbContextSettings.CreateDefault(options.DefaultConnectionName)); return(settings.ConnectionString.IfNullOrWhiteSpace(options.GetConnectionString(settings.ConnectionName))); }
public static IDbProvider GetDbProvider(this DbOptions options, Type dbContextType) { var settings = options.DbContextSettings.GetOrDefault(dbContextType, () => DbContextSettings.CreateDefault(options.DefaultConnectionName)); return(settings.DbProvider); }
public static bool IncludeBuiltinEntities(this DbOptions options, Type dbContextType) { var settings = options.DbContextSettings.GetOrDefault(dbContextType, () => DbContextSettings.CreateDefault(options.DefaultConnectionName)); return(settings.IncludeServiceEntities); }
public static bool IsDefaultContext(this DbOptions options, Type dbContextType) { var settings = options.DbContextSettings.GetOrDefault(dbContextType, () => DbContextSettings.CreateDefault(options.DefaultConnectionName)); return(settings.IsDefault); }
public static IEnumerable <String> GetTablesForChecking(this DbOptions options, Type dbContextType) { var settings = options.DbContextSettings.GetOrDefault(dbContextType, () => DbContextSettings.CreateDefault(options.DefaultConnectionName)); return(settings.TablesForCheckingDatabaseExists); }
public static String GetMigrationAssembly(this DbOptions options, Type dbContextType) { var settings = options.DbContextSettings.GetOrDefault(dbContextType, () => DbContextSettings.CreateDefault(options.DefaultConnectionName)); return(settings.MigrationAssembly.IfNullOrWhiteSpace(dbContextType.GetTypeInfo().Assembly.FullName)); }
public DbSettingsrExtension(DbContextSettings settings) { Guard.ArgumentNotNull(settings, nameof(settings)); _settings = settings; }
private DbBuilder AddDbContext <TContext>(String dbConnectionName, String connectionString, Action <DbContextSettingsBuilder> setup) where TContext : DbContext, INewDatabaseFlag { if (dbConnectionName.IsNullOrWhiteSpace() && connectionString.IsNullOrWhiteSpace()) { throw new ArgumentException($"必须提供非空的 {nameof(dbConnectionName)} 或 {nameof(connectionString)}。"); } String connName = dbConnectionName; if (connName.IsNullOrWhiteSpace()) { connName = Guid.NewGuid().ToString(); this.Setup += ((DbOptions innerOptions) => { innerOptions.ConnectionStrings.Add(connName, connectionString); }); } this.DbContexts[connName] = typeof(TContext); DbContextSettingsBuilder builder = new DbContextSettingsBuilder(connName, connectionString); setup?.Invoke(builder); SetDefaultDbContext <TContext>(builder); DbContextSettings settings = builder.Build(); this.DbSettings[typeof(TContext)] = settings; if (!this.DbConfigurings.ContainsKey(settings.DbProvider.GetType())) { this.DbConfigurings[settings.DbProvider.GetType()] = services => settings.DbProvider.OnAddDependencies(services); } this.DbConfigurings[typeof(TContext)] = c => { var configure = new Action <IServiceProvider, DbContextOptionsBuilder>((sp, b) => { var options = sp.GetRequiredService <IOptions <DbOptions> >(); settings.DbProvider.OnBuildContext(typeof(TContext), b, options.Value); b.UseLoggerFactory(sp.GetRequiredService <ILoggerFactory>()); b.Options.WithExtension <IDbContextOptionsExtension>(new DbSettingsrExtension(settings)); b.ReplaceService <IModelCustomizer, SchubertModelCustomizer>(); }); if (settings.UsePool) { c.AddDbContextPool <TContext>(configure, settings.PoolSize); } else { c.AddDbContext <TContext>(optionsAction: configure); } }; if (builder.IsDefaultDbContext) { this.DbConfigurings[typeof(TContext)] += services => { services.AddScoped(typeof(DbContext), s => (DbContext)s.GetRequiredService(typeof(TContext))); }; } return(this); }