private void InitDatabase() { //初始化数据库 using (var database = new SqlServerDb()) //新增 { database.Database.EnsureDeleted(); database.Database.EnsureCreated(); } var services = new ServiceCollection(); services.AddSummerBoot(); var connectionString = MyConfiguration.GetConfiguration("sqlServerDbConnectionString"); if (string.IsNullOrWhiteSpace(connectionString)) { throw new ArgumentNullException("Sqlserver connectionString must not be null"); } services.AddSummerBootRepository(it => { it.DbConnectionType = typeof(SqlConnection); it.ConnectionString = connectionString; }); serviceProvider = services.BuildServiceProvider(); serviceProvider = serviceProvider.CreateScope().ServiceProvider; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { var connectionString = MyConfiguration.GetConfiguration("sqlServerDbConnectionString"); if (string.IsNullOrWhiteSpace(connectionString)) { throw new ArgumentNullException("sqlServer connectionString must not be null"); } optionsBuilder.UseSqlServer(connectionString); }