2022-04-28 16:31:07 +08:00
|
|
|
using System;
|
2021-09-22 21:55:52 +08:00
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
2021-11-07 11:29:37 +08:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
2021-09-22 21:55:52 +08:00
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
using Microsoft.Extensions.Logging;
|
2021-11-07 11:29:37 +08:00
|
|
|
using Sample.SqlServerShardingDataSource.VirtualRoutes;
|
2021-09-22 21:55:52 +08:00
|
|
|
using ShardingCore;
|
2021-11-07 11:29:37 +08:00
|
|
|
using System.Collections.Generic;
|
2022-04-28 16:31:07 +08:00
|
|
|
using System.Diagnostics;
|
|
|
|
using System.Linq;
|
2021-12-14 14:44:14 +08:00
|
|
|
using ShardingCore.TableExists;
|
2022-07-03 16:52:03 +08:00
|
|
|
using ShardingCore.TableExists.Abstractions;
|
2021-09-22 21:55:52 +08:00
|
|
|
|
|
|
|
namespace Sample.SqlServerShardingDataSource
|
|
|
|
{
|
|
|
|
public class Startup
|
|
|
|
{
|
|
|
|
public static readonly ILoggerFactory efLogger = LoggerFactory.Create(builder =>
|
|
|
|
{
|
|
|
|
builder.AddFilter((category, level) => category == DbLoggerCategory.Database.Command.Name && level == LogLevel.Information).AddConsole();
|
|
|
|
});
|
|
|
|
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();
|
|
|
|
|
2022-01-06 21:30:05 +08:00
|
|
|
services.AddShardingDbContext<MyDbContext>()
|
2022-07-03 16:52:03 +08:00
|
|
|
.UseRouteConfig(o =>
|
2021-09-23 10:17:25 +08:00
|
|
|
{
|
2022-01-06 21:30:05 +08:00
|
|
|
o.AddShardingDataSourceRoute<OrderVirtualDataSourceRoute>();
|
|
|
|
o.AddShardingDataSourceRoute<SysUserVirtualDataSourceRoute>();
|
|
|
|
})
|
2022-07-03 16:52:03 +08:00
|
|
|
.UseConfig(op =>
|
2021-09-22 21:55:52 +08:00
|
|
|
{
|
2022-01-06 21:30:05 +08:00
|
|
|
op.UseShardingQuery((conStr, builder) =>
|
|
|
|
{
|
|
|
|
builder.UseSqlServer(conStr).UseLoggerFactory(efLogger);
|
|
|
|
});
|
|
|
|
op.UseShardingTransaction((connection, builder) =>
|
|
|
|
{
|
|
|
|
builder.UseSqlServer(connection).UseLoggerFactory(efLogger);
|
|
|
|
});
|
2022-04-28 16:31:07 +08:00
|
|
|
op.AddDefaultDataSource("00",
|
|
|
|
"Data Source=localhost;Initial Catalog=EFCoreShardingDataSourceOnly00;Integrated Security=True;");
|
2022-01-06 21:30:05 +08:00
|
|
|
op.AddExtraDataSource(sp =>
|
|
|
|
{
|
2022-08-17 11:08:27 +08:00
|
|
|
return Enumerable.Range(1, 3).Select(o => o.ToString().PadLeft(2, '0')).ToList()
|
2022-04-28 16:31:07 +08:00
|
|
|
.ToDictionary(o => o,
|
|
|
|
o =>
|
|
|
|
$"Data Source=localhost;Initial Catalog=EFCoreShardingDataSourceOnly{o};Integrated Security=True;");
|
2022-01-06 21:30:05 +08:00
|
|
|
});
|
2022-07-03 16:52:03 +08:00
|
|
|
}).ReplaceService<ITableEnsureManager,SqlServerTableEnsureManager>().AddShardingCore();
|
2021-09-22 21:55:52 +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();
|
|
|
|
});
|
2022-08-17 11:29:34 +08:00
|
|
|
app.InitSeed();
|
2021-09-22 21:55:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|