2021-01-26 14:39:56 +08:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2021-08-17 22:17:18 +08:00
|
|
|
using Microsoft.Data.SqlClient;
|
2021-08-15 07:32:50 +08:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
2021-08-17 22:17:18 +08:00
|
|
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
2021-01-26 14:39:56 +08:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
using Microsoft.Extensions.Hosting;
|
2021-08-17 22:17:18 +08:00
|
|
|
using Microsoft.Extensions.Logging;
|
2021-03-05 16:55:52 +08:00
|
|
|
using Sample.SqlServer.DbContexts;
|
2021-01-26 14:39:56 +08:00
|
|
|
using Sample.SqlServer.Shardings;
|
2021-08-17 22:17:18 +08:00
|
|
|
using ShardingCore;
|
|
|
|
using ShardingCore.EFCores;
|
2021-01-26 14:39:56 +08:00
|
|
|
using ShardingCore.SqlServer;
|
|
|
|
|
|
|
|
namespace Sample.SqlServer
|
|
|
|
{
|
|
|
|
public class Startup
|
|
|
|
{
|
2021-08-17 22:17:18 +08:00
|
|
|
public static readonly ILoggerFactory efLogger = LoggerFactory.Create(builder =>
|
|
|
|
{
|
|
|
|
builder.AddFilter((category, level) => category == DbLoggerCategory.Database.Command.Name && level == LogLevel.Information).AddConsole();
|
|
|
|
});
|
2021-01-26 14:39:56 +08:00
|
|
|
// This method gets called by the runtime. Use this method to add services to the container.
|
|
|
|
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
|
|
|
|
public void ConfigureServices(IServiceCollection services)
|
|
|
|
{
|
|
|
|
services.AddControllers();
|
|
|
|
services.AddShardingSqlServer(o =>
|
|
|
|
{
|
2021-08-17 22:17:18 +08:00
|
|
|
o.EnsureCreatedWithOutShardingTable = false;
|
|
|
|
o.CreateShardingTableOnStart = false;
|
2021-08-15 07:32:50 +08:00
|
|
|
o.UseShardingDbContext<DefaultTableDbContext>( dbConfig =>
|
2021-01-26 14:39:56 +08:00
|
|
|
{
|
2021-03-13 13:32:43 +08:00
|
|
|
dbConfig.AddShardingTableRoute<SysUserModVirtualTableRoute>();
|
2021-01-26 14:39:56 +08:00
|
|
|
});
|
2021-03-05 16:55:52 +08:00
|
|
|
//o.AddDataSourceVirtualRoute<>();
|
|
|
|
|
2021-01-26 14:39:56 +08:00
|
|
|
});
|
2021-08-16 04:21:46 +08:00
|
|
|
services.AddDbContext<DefaultTableDbContext>(o => o.UseSqlServer("Data Source=localhost;Initial Catalog=ShardingCoreDB;Integrated Security=True"));
|
|
|
|
|
|
|
|
|
2021-08-17 22:17:18 +08:00
|
|
|
services.AddShardingDbContext<DefaultShardingDbContext, DefaultTableDbContext>(op =>
|
|
|
|
{
|
|
|
|
op.UseShardingDbContextOptions((connection, builder) =>
|
|
|
|
{
|
|
|
|
return builder.UseSqlServer(connection).UseLoggerFactory(efLogger)
|
|
|
|
.UseQueryTrackingBehavior(QueryTrackingBehavior.TrackAll)
|
|
|
|
.ReplaceService<IModelCacheKeyFactory, ShardingModelCacheKeyFactory>()
|
|
|
|
.ReplaceService<IModelCustomizer, ShardingModelCustomizer>().Options;
|
|
|
|
});
|
|
|
|
},o =>
|
|
|
|
o.UseSqlServer("Data Source=localhost;Initial Catalog=ShardingCoreDB;Integrated Security=True;MultipleActiveResultSets=True;").UseSharding());
|
2021-01-26 14:39:56 +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();
|
|
|
|
|
2021-03-08 14:59:32 +08:00
|
|
|
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
|
2021-08-16 04:21:46 +08:00
|
|
|
//app.DbSeed();
|
2021-01-26 14:39:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|