示例#1
0
        public static IServiceCollection AddEfCoreSqlServerDb(this IServiceCollection services)
        {
            services.AddDbContextPool <DbContext, AppDbContext>((sp, o) =>
            {
                IExtendDbContextOptionsBuilder extendOptionsBuilder =
                    sp.GetRequiredService <IExtendDbContextOptionsBuilder>();

                IDbConnStringFactory connStringFactory =
                    sp.GetRequiredService <IDbConnStringFactory>();

                extendOptionsBuilder.Extend(o, connStringFactory, string.Empty);
            });

            services.Replace(
                ServiceDescriptor.Scoped <
                    IDbConnStringFactory,
                    SqlServerConnectionStringFactory>());

            services.Replace(
                ServiceDescriptor.Scoped <
                    IExtendDbContextOptionsBuilder,
                    SqlServerDbContextOptionsBuilder>());

            services.Replace(
                ServiceDescriptor.Scoped <
                    IRepositoryFactory,
                    DefaultRepositoryFactory>());

            services.Replace(
                ServiceDescriptor.Scoped <
                    DatabaseMigration,
                    SqlServerDatabaseMigration>());

            return(services);
        }
        public ApplicationDbContext CreateDbContext(string[] args)
        {
            var migrationAssembly = typeof(FactoryForApplicationDb).GetTypeInfo().Assembly;

            var dbContextOptionBuilder = _extendOptionsBuilder.Extend(
                new DbContextOptionsBuilder <ApplicationDbContext>(),
                _dbConnectionStringFactory,
                migrationAssembly.GetName().Name);

            return((ApplicationDbContext)Activator.CreateInstance(
                       typeof(ApplicationDbContext),
                       dbContextOptionBuilder.Options));
        }
示例#3
0
        public static IServiceCollection AddMiniService(this IServiceCollection services, Assembly startupAssembly)
        {
            ServiceProvider serviceProvider = services.BuildServiceProvider();
            IConfiguration  config          = serviceProvider.GetService <IConfiguration>();
            IExtendDbContextOptionsBuilder   extendOptionsBuilder      = serviceProvider.GetService <IExtendDbContextOptionsBuilder>();
            IDatabaseConnectionStringFactory dbConnectionStringFactory = serviceProvider.GetService <IDatabaseConnectionStringFactory>();

            services.AddEfCore();

            services.AddRouting(options => options.LowercaseUrls = true);
            services.AddOptions()
            .Configure <PersistenceOption>(config.GetSection("EfCore"));

            void optionsBuilderAction(DbContextOptionsBuilder optionsBuilder)
            {
                extendOptionsBuilder.Extend(
                    optionsBuilder,
                    dbConnectionStringFactory,
                    startupAssembly.GetName().Name);
            }

            services.AddDbContext <ApplicationDbContext>(options => optionsBuilderAction(options));
            services.AddScoped <DbContext>(resolver => resolver.GetRequiredService <ApplicationDbContext>());

            services.AddHttpContextAccessor();
            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();
            services.AddSingleton <IActionContextAccessor, ActionContextAccessor>();
            services.AddScoped <IUrlHelper>(implementationFactory =>
            {
                ActionContext actionContext = implementationFactory.GetService <IActionContextAccessor>().ActionContext;
                return(new UrlHelper(actionContext));
            });
            services.AddSingleton(typeof(RestClient), typeof(RestClient));

            services.AddMvcCore().AddVersionedApiExplorer(
                options =>
            {
                options.GroupNameFormat           = "'v'VVV";
                options.SubstituteApiVersionInUrl = true;
            });

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            services.AddApiVersioning(o =>
            {
                o.ReportApiVersions = true;
            });

            return(services);
        }
示例#4
0
        public ConfigurationDbContext CreateDbContext(string[] args)
        {
            var migrationAssembly = typeof(ContextFactoryForConfigurationDb).GetTypeInfo().Assembly;

            var dbContextOptionBuilder = _extendOptionsBuilder.Extend(
                new DbContextOptionsBuilder <ConfigurationDbContext>(),
                _dbConnectionStringFactory.Create(),
                migrationAssembly.GetName().Name);

            return((ConfigurationDbContext)Activator.CreateInstance(
                       typeof(ConfigurationDbContext),
                       dbContextOptionBuilder.Options,
                       new ConfigurationStoreOptions()));
        }
示例#5
0
        public PersistedGrantDbContext CreateDbContext(string[] args)
        {
            var migrationAssembly = typeof(FactoryForPersistedGrantDb).GetTypeInfo().Assembly;

            var dbContextOptionBuilder = _extendOptionsBuilder.Extend(
                new DbContextOptionsBuilder <PersistedGrantDbContext>(),
                _dbConnectionStringFactory,
                migrationAssembly.GetName().Name);

            return((PersistedGrantDbContext)Activator.CreateInstance(
                       typeof(PersistedGrantDbContext),
                       dbContextOptionBuilder.Options,
                       new OperationalStoreOptions()));
        }
        public TDbConext CreateDbContext(string[] args)
        {
            var migrationAssembly = Assembly.GetEntryAssembly();

            var dbContextOptionBuilder = ExtendOptionsBuilder.Extend(
                new DbContextOptionsBuilder <TDbConext>(),
                DbConnectionStringFactory.Create(),
                migrationAssembly.GetName().Name);

            if (SetOptions() == null)
            {
                return((TDbConext)Activator.CreateInstance(
                           typeof(TDbConext),
                           dbContextOptionBuilder.Options));
            }

            return((TDbConext)Activator.CreateInstance(
                       typeof(TDbConext),
                       dbContextOptionBuilder.Options,
                       SetOptions()));
        }