2021-03-22 11:12:00 +08:00
using Microsoft.AspNetCore.Builder ;
using Microsoft.AspNetCore.Hosting ;
using Microsoft.AspNetCore.Mvc ;
using Microsoft.Extensions.Configuration ;
using Microsoft.Extensions.DependencyInjection ;
using Microsoft.Extensions.Hosting ;
using Microsoft.Extensions.Logging ;
using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Threading.Tasks ;
2021-03-22 14:43:36 +08:00
using Microsoft.EntityFrameworkCore ;
2021-03-22 11:12:00 +08:00
using Sample.MySql.DbContexts ;
using Sample.MySql.Shardings ;
using ShardingCore.MySql ;
namespace Sample.MySql
{
public class Startup
{
public Startup ( IConfiguration configuration )
{
Configuration = configuration ;
}
public IConfiguration Configuration { get ; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices ( IServiceCollection services )
{
services . AddControllers ( ) ;
services . AddShardingMySql ( o = >
{
o . EnsureCreatedWithOutShardingTable = true ;
o . CreateShardingTableOnStart = true ;
2021-08-15 07:32:50 +08:00
o . UseShardingDbContext < DefaultTableDbContext > ( dbConfig = >
2021-03-22 11:12:00 +08:00
{
dbConfig . AddShardingTableRoute < SysUserModVirtualTableRoute > ( ) ;
2021-03-22 14:43:36 +08:00
dbConfig . AddShardingTableRoute < SysUserLogByMonthRoute > ( ) ;
2021-03-22 11:12:00 +08:00
} ) ;
//o.AddDataSourceVirtualRoute<>();
2021-03-23 17:42:37 +08:00
o . IgnoreCreateTableError = true ;
2021-03-22 11:12:00 +08:00
} ) ;
2021-08-15 07:32:50 +08:00
services . AddDbContext < DefaultTableDbContext > ( o = > o . UseMySql ( "server=xxx;userid=xxx;password=xxx;database=sharding_db123;Charset=utf8;Allow Zero Datetime=True; Pooling=true; Max Pool Size=512;sslmode=none;Allow User Variables=True;" , o = >
{
o . ServerVersion ( "5.7.13" ) ;
} ) . UseShardingMySqlUpdateSqlGenerator ( ) ) ;
2021-03-22 14:43:36 +08:00
services . AddLogging ( b = > b . AddConsole ( ) ) ;
2021-03-22 11:12:00 +08:00
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure ( IApplicationBuilder app , IWebHostEnvironment env )
{
if ( env . IsDevelopment ( ) )
{
app . UseDeveloperExceptionPage ( ) ;
}
app . UseShardingCore ( ) ;
app . UseRouting ( ) ;
app . UseAuthorization ( ) ;
app . UseEndpoints ( endpoints = >
{
endpoints . MapControllers ( ) ;
} ) ;
app . DbSeed ( ) ;
}
}
}