protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            var config = new Microsoft.Extensions.Configuration.ConfigurationBuilder()
                         .SetBasePath(System.IO.Directory.GetCurrentDirectory())
                         .AddJsonFile(Common.Utility.Const.CONST_FILE_APPSETTING)
                         .Build();

            // define the database to use
            optionsBuilder.UseSqlServer(config.GetConnectionString(Common.Utility.Const.CONST_CHAT_CONNECTION));
        }
Exemple #2
0
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();

            IConfiguration config = new ConfigurationBuilder().
            SetBasePath(Directory.GetCurrentDirectory()).
            AddJsonFile("appsettings.json").
            Build();

            string connectionString = config.GetConnectionString("ContosoDatabase");
            services.AddDbContext<ContosoContext>(options => options.UseSqlServer(connectionString));
        }
Exemple #3
0
        /// <inheritdoc />
        public ApplicationDbContext CreateDbContext(string[] args)
        {
            var configuration = new ConfigurationBuilder()
                                .SetBasePath(Directory.GetCurrentDirectory())
                                .AddJsonFile("appsettings.json")
                                .Build();
            var builder          = new DbContextOptionsBuilder <ApplicationDbContext>();
            var connectionString = configuration.GetConnectionString(ConfigurationVariables.DefaultConnection);

            builder.UseSqlServer(connectionString, sqlServerOptions =>
                                 sqlServerOptions.MigrationsAssembly(ConfigurationVariables.MigrationAssembly));
            return(new ApplicationDbContext(builder.Options));
        }
Exemple #4
0
        public static string GetConnectionString()
        {
            string environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

            IConfigurationRoot configuration = new Microsoft.Extensions.Configuration.ConfigurationBuilder()
                                               .SetBasePath(Directory.GetCurrentDirectory())
                                               .AddJsonFile("appsettings.json")
                                               .AddJsonFile($"appsettings.{environmentName}.json", true)
                                               .AddEnvironmentVariables().Build();

            string connectionString = configuration.GetConnectionString("DefaultConnection");

            return(connectionString);
        }
Exemple #5
0
        public WebAppContext CreateDbContext(string[] args)
        {
            Microsoft.Extensions.Configuration.IConfigurationRoot configuration = new Microsoft.Extensions.Configuration.ConfigurationBuilder()
                                                                                  .SetBasePath(Directory.GetCurrentDirectory())
                                                                                  .AddJsonFile("appsettings.json")
                                                                                  .Build();

            var builder = new DbContextOptionsBuilder <WebAppContext>();

            var connectionString = configuration.GetConnectionString("DefaultConnection");

            builder.UseSqlServer(connectionString);

            return(new WebAppContext(builder.Options));
        }