Exemplo n.º 1
0
        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)));
        }
Exemplo n.º 2
0
        public static IDbProvider GetDbProvider(this DbOptions options, Type dbContextType)
        {
            var settings = options.DbContextSettings.GetOrDefault(dbContextType, () => DbContextSettings.CreateDefault(options.DefaultConnectionName));

            return(settings.DbProvider);
        }
Exemplo n.º 3
0
        public static bool IncludeBuiltinEntities(this DbOptions options, Type dbContextType)
        {
            var settings = options.DbContextSettings.GetOrDefault(dbContextType, () => DbContextSettings.CreateDefault(options.DefaultConnectionName));

            return(settings.IncludeServiceEntities);
        }
Exemplo n.º 4
0
        public static bool IsDefaultContext(this DbOptions options, Type dbContextType)
        {
            var settings = options.DbContextSettings.GetOrDefault(dbContextType, () => DbContextSettings.CreateDefault(options.DefaultConnectionName));

            return(settings.IsDefault);
        }
Exemplo n.º 5
0
        public static IEnumerable <String> GetTablesForChecking(this DbOptions options, Type dbContextType)
        {
            var settings = options.DbContextSettings.GetOrDefault(dbContextType, () => DbContextSettings.CreateDefault(options.DefaultConnectionName));

            return(settings.TablesForCheckingDatabaseExists);
        }
Exemplo n.º 6
0
        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));
        }
Exemplo n.º 7
0
 public DbSettingsrExtension(DbContextSettings settings)
 {
     Guard.ArgumentNotNull(settings, nameof(settings));
     _settings = settings;
 }
Exemplo n.º 8
0
        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);
        }