sharding/samples/Sample.SqlServerShardingTable/Startup.cs

139 lines
5.8 KiB
C#
Raw Normal View History

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.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Sample.SqlServerShardingTable.Entities;
using Sample.SqlServerShardingTable.VirtualRoutes;
using ShardingCore;
using ShardingCore.Sharding.ReadWriteConfigurations;
2022-01-06 21:30:05 +08:00
using ShardingCore.TableExists;
using ShardingCore.TableExists.Abstractions;
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 =>
// {
// //如果您使用code-first建议选择false
2022-01-06 14:51:01 +08:00
// op.CreateShardingTableOnStart = true;
// //如果您使用code-first建议修改为fsle
2022-01-06 14:51:01 +08:00
// op.EnsureCreatedWithOutShardingTable = true;
// //当无法获取路由时会返回默认值而不是报错
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
{
//当无法获取路由时会返回默认值而不是报错
op.ThrowIfQueryRouteNotMatch = false;
2022-01-06 14:51:01 +08:00
op.UseShardingQuery((conStr, builder) =>
{
builder.UseSqlServer(conStr).UseLoggerFactory(efLogger);
2022-01-06 14:51:01 +08:00
});
op.UseShardingTransaction((connection, builder) =>
{
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 =>
{
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;"
}
}
};
2022-01-06 14:51:01 +08:00
}, ReadStrategyEnum.Loop, defaultEnable: true);
2022-11-11 22:47:18 +08:00
}).AddShardingCore();
}
// 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-13 13:20:39 +08:00
//建议补偿表在迁移后面
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();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
app.InitSeed();
}
}
}