2021-11-07 11:29:37 +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;
|
2021-12-30 12:35:24 +08:00
|
|
|
using System.Collections.Concurrent;
|
2021-11-07 11:29:37 +08:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2021-12-28 15:42:31 +08:00
|
|
|
using Sample.SqlServerShardingTable.Entities;
|
2021-11-07 11:29:37 +08:00
|
|
|
using Sample.SqlServerShardingTable.VirtualRoutes;
|
|
|
|
using ShardingCore;
|
2021-12-30 12:35:24 +08:00
|
|
|
using ShardingCore.Sharding.ReadWriteConfigurations;
|
2022-01-06 21:30:05 +08:00
|
|
|
using ShardingCore.TableExists;
|
2022-07-03 16:52:03 +08:00
|
|
|
using ShardingCore.TableExists.Abstractions;
|
2021-11-07 11:29:37 +08:00
|
|
|
|
|
|
|
namespace Sample.SqlServerShardingTable
|
|
|
|
{
|
|
|
|
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 14:51:01 +08:00
|
|
|
//services.AddShardingDbContext<MyDbContext>((conStr, builder) =>
|
|
|
|
// {
|
|
|
|
// builder.UseSqlServer(conStr).UseLoggerFactory(efLogger);
|
|
|
|
// }).Begin(op =>
|
|
|
|
// {
|
2022-07-03 16:52:03 +08:00
|
|
|
// //如果您使用code-first建议选择false
|
2022-01-06 14:51:01 +08:00
|
|
|
// op.CreateShardingTableOnStart = true;
|
2022-07-03 16:52:03 +08:00
|
|
|
// //如果您使用code-first建议修改为fsle
|
2022-01-06 14:51:01 +08:00
|
|
|
// op.EnsureCreatedWithOutShardingTable = true;
|
2022-07-03 16:52:03 +08:00
|
|
|
// //当无法获取路由时会返回默认值而不是报错
|
2022-01-06 14:51:01 +08:00
|
|
|
// op.ThrowIfQueryRouteNotMatch = true;
|
|
|
|
// }).AddShardingTransaction((connection, builder) =>
|
|
|
|
// {
|
|
|
|
// builder.UseSqlServer(connection).UseLoggerFactory(efLogger);
|
|
|
|
// }).AddDefaultDataSource("ds0",
|
|
|
|
// "Data Source=localhost;Initial Catalog=EFCoreShardingTableDB;Integrated Security=True;")
|
|
|
|
// .AddShardingTableRoute(op =>
|
|
|
|
// {
|
|
|
|
// op.AddShardingTableRoute<SysUserVirtualTableRoute>();
|
|
|
|
// op.AddShardingTableRoute<OrderVirtualTableRoute>();
|
|
|
|
// op.AddShardingTableRoute<MultiShardingOrderVirtualTableRoute>();
|
|
|
|
// }).AddReadWriteSeparation(sp =>
|
|
|
|
// {
|
|
|
|
// return new Dictionary<string, IEnumerable<string>>()
|
|
|
|
// {
|
|
|
|
// {
|
|
|
|
// "ds0", new List<string>()
|
|
|
|
// {
|
|
|
|
// "Data Source=localhost;Initial Catalog=EFCoreShardingTableDB;Integrated Security=True;"
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// };
|
|
|
|
// },ReadStrategyEnum.Loop,defaultEnable:true).End();
|
2022-11-11 22:47:18 +08:00
|
|
|
services.AddShardingDbContext<MyDbContext>().UseRouteConfig(op =>
|
2022-01-06 14:51:01 +08:00
|
|
|
{
|
|
|
|
op.AddShardingTableRoute<SysUserVirtualTableRoute>();
|
|
|
|
op.AddShardingTableRoute<OrderVirtualTableRoute>();
|
|
|
|
op.AddShardingTableRoute<MultiShardingOrderVirtualTableRoute>();
|
2022-11-11 22:47:18 +08:00
|
|
|
}).UseConfig(op =>
|
2022-01-06 14:51:01 +08:00
|
|
|
{
|
2022-07-06 10:39:28 +08:00
|
|
|
//当无法获取路由时会返回默认值而不是报错
|
|
|
|
op.ThrowIfQueryRouteNotMatch = false;
|
2022-01-06 14:51:01 +08:00
|
|
|
op.UseShardingQuery((conStr, builder) =>
|
2021-11-07 11:29:37 +08:00
|
|
|
{
|
|
|
|
builder.UseSqlServer(conStr).UseLoggerFactory(efLogger);
|
2022-01-06 14:51:01 +08:00
|
|
|
});
|
|
|
|
op.UseShardingTransaction((connection, builder) =>
|
2021-11-07 11:29:37 +08:00
|
|
|
{
|
|
|
|
builder.UseSqlServer(connection).UseLoggerFactory(efLogger);
|
2022-01-06 14:51:01 +08:00
|
|
|
});
|
|
|
|
op.AddDefaultDataSource("ds0",
|
|
|
|
"Data Source=localhost;Initial Catalog=EFCoreShardingTableDB;Integrated Security=True;");
|
|
|
|
op.AddReadWriteSeparation(sp =>
|
2021-12-31 15:31:48 +08:00
|
|
|
{
|
|
|
|
return new Dictionary<string, IEnumerable<string>>()
|
|
|
|
{
|
2022-11-11 22:47:18 +08:00
|
|
|
{
|
|
|
|
"ds0", new List<string>()
|
|
|
|
{
|
|
|
|
"Data Source=localhost;Initial Catalog=EFCoreShardingTableDB;Integrated Security=True;"
|
|
|
|
}
|
|
|
|
}
|
2021-12-31 15:31:48 +08:00
|
|
|
};
|
2022-01-06 14:51:01 +08:00
|
|
|
}, ReadStrategyEnum.Loop, defaultEnable: true);
|
2022-11-11 22:47:18 +08:00
|
|
|
}).AddShardingCore();
|
2021-11-07 11:29:37 +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-11-11 22:47:18 +08:00
|
|
|
using (var scope = app.ApplicationServices.CreateScope())
|
|
|
|
{
|
|
|
|
var myDbContext = scope.ServiceProvider.GetService<MyDbContext>();
|
|
|
|
//如果没有迁移那么就直接创建表和库
|
|
|
|
myDbContext.Database.EnsureCreated();
|
|
|
|
//如果有迁移使用下面的
|
|
|
|
// myDbContext.Database.Migrate();
|
|
|
|
}
|
|
|
|
app.ApplicationServices.UseAutoTryCompensateTable();
|
|
|
|
// app.UseShardingCore();
|
2021-11-07 11:29:37 +08:00
|
|
|
app.UseRouting();
|
|
|
|
|
|
|
|
app.UseAuthorization();
|
|
|
|
|
|
|
|
app.UseEndpoints(endpoints =>
|
|
|
|
{
|
|
|
|
endpoints.MapControllers();
|
|
|
|
});
|
|
|
|
app.InitSeed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|