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;
|
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>()
|
|
|
|
|
.AddEntityConfig(o =>
|
2021-09-23 10:17:25 +08:00
|
|
|
|
{
|
2022-04-28 16:31:07 +08:00
|
|
|
|
o.CreateShardingTableOnStart = false;
|
|
|
|
|
o.EnsureCreatedWithOutShardingTable = false;
|
2022-01-06 21:30:05 +08:00
|
|
|
|
o.AddShardingDataSourceRoute<OrderVirtualDataSourceRoute>();
|
|
|
|
|
o.AddShardingDataSourceRoute<SysUserVirtualDataSourceRoute>();
|
|
|
|
|
})
|
|
|
|
|
.AddConfig(op =>
|
2021-09-22 21:55:52 +08:00
|
|
|
|
{
|
2022-01-06 21:30:05 +08:00
|
|
|
|
op.ConfigId = "c1";
|
|
|
|
|
op.UseShardingQuery((conStr, builder) =>
|
|
|
|
|
{
|
|
|
|
|
builder.UseSqlServer(conStr).UseLoggerFactory(efLogger);
|
|
|
|
|
});
|
|
|
|
|
op.UseShardingTransaction((connection, builder) =>
|
|
|
|
|
{
|
|
|
|
|
builder.UseSqlServer(connection).UseLoggerFactory(efLogger);
|
|
|
|
|
});
|
|
|
|
|
op.ReplaceTableEnsureManager(sp => new SqlServerTableEnsureManager<MyDbContext>());
|
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-04-28 16:31:07 +08:00
|
|
|
|
return Enumerable.Range(1, 100).Select(o => (o % 100).ToString().PadLeft(2, '0')).ToList()
|
|
|
|
|
.ToDictionary(o => o,
|
|
|
|
|
o =>
|
|
|
|
|
$"Data Source=localhost;Initial Catalog=EFCoreShardingDataSourceOnly{o};Integrated Security=True;");
|
2022-01-06 21:30:05 +08:00
|
|
|
|
});
|
|
|
|
|
}).EnsureConfig();
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-28 16:31:07 +08:00
|
|
|
|
Stopwatch sp=Stopwatch.StartNew();
|
2021-09-22 21:55:52 +08:00
|
|
|
|
app.UseShardingCore();
|
2022-04-28 16:31:07 +08:00
|
|
|
|
sp.Stop();
|
|
|
|
|
Console.WriteLine("<22><>ʱ"+sp.ElapsedMilliseconds);
|
2021-09-22 21:55:52 +08:00
|
|
|
|
app.UseRouting();
|
|
|
|
|
|
|
|
|
|
app.UseAuthorization();
|
|
|
|
|
|
|
|
|
|
app.UseEndpoints(endpoints =>
|
|
|
|
|
{
|
|
|
|
|
endpoints.MapControllers();
|
|
|
|
|
});
|
2022-04-28 16:31:07 +08:00
|
|
|
|
//app.InitSeed();
|
2021-09-22 21:55:52 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|