2021-01-26 14:39:56 +08:00
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
2021-08-15 07:32:50 +08:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
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;
|
2021-11-30 09:53:07 +08:00
|
|
|
using ShardingCore.Sharding.ReadWriteConfigurations;
|
2021-11-03 08:48:24 +08:00
|
|
|
using System;
|
2021-11-13 11:13:47 +08:00
|
|
|
using System.Collections.Generic;
|
2021-12-12 13:40:32 +08:00
|
|
|
using System.Diagnostics;
|
2021-12-06 23:46:13 +08:00
|
|
|
using ShardingCore.Core;
|
2021-12-12 13:40:32 +08:00
|
|
|
using ShardingCore.TableExists;
|
2021-01-26 14:39:56 +08:00
|
|
|
|
|
|
|
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();
|
2021-08-20 17:36:15 +08:00
|
|
|
//services.AddDbContext<DefaultTableDbContext>(o => o.UseSqlServer("Data Source=localhost;Initial Catalog=ShardingCoreDBxx3;Integrated Security=True"));
|
2021-08-16 04:21:46 +08:00
|
|
|
|
2021-10-11 20:58:55 +08:00
|
|
|
services.AddShardingDbContext<DefaultShardingDbContext>(
|
2021-10-15 17:18:23 +08:00
|
|
|
(conn, o) =>
|
|
|
|
o.UseSqlServer(conn).UseLoggerFactory(efLogger)
|
2021-09-23 10:17:25 +08:00
|
|
|
).Begin(o =>
|
|
|
|
{
|
|
|
|
o.CreateShardingTableOnStart = true;
|
|
|
|
o.EnsureCreatedWithOutShardingTable = true;
|
2021-12-06 23:46:13 +08:00
|
|
|
o.MaxQueryConnectionsLimit = Environment.ProcessorCount;
|
|
|
|
o.ConnectionMode = ConnectionModeEnum.SYSTEM_AUTO;
|
2021-11-12 16:13:25 +08:00
|
|
|
//if SysTest entity not exists in db and db is exists
|
|
|
|
//o.AddEntityTryCreateTable<SysTest>(); // or `o.AddEntitiesTryCreateTable(typeof(SysTest));`
|
2021-09-23 10:17:25 +08:00
|
|
|
})
|
2021-10-15 17:18:23 +08:00
|
|
|
//.AddShardingQuery((conStr, builder) => builder.UseSqlServer(conStr).UseLoggerFactory(efLogger))//无需添加.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking) 并发查询系统会自动添加NoTracking
|
2021-10-20 11:08:44 +08:00
|
|
|
.AddShardingTransaction((connection, builder) =>
|
|
|
|
builder.UseSqlServer(connection).UseLoggerFactory(efLogger))
|
2021-12-02 23:47:26 +08:00
|
|
|
.AddDefaultDataSource("A",
|
2021-12-05 01:15:45 +08:00
|
|
|
"Data Source=localhost;Initial Catalog=ShardingCoreDBXA;Integrated Security=True;")
|
2021-09-23 10:17:25 +08:00
|
|
|
.AddShardingTableRoute(o =>
|
2021-09-20 22:26:48 +08:00
|
|
|
{
|
|
|
|
o.AddShardingTableRoute<SysUserModVirtualTableRoute>();
|
|
|
|
o.AddShardingTableRoute<SysUserSalaryVirtualTableRoute>();
|
2021-11-04 17:42:26 +08:00
|
|
|
o.AddShardingTableRoute<TestYearShardingVirtualTableRoute>();
|
2021-12-12 13:40:32 +08:00
|
|
|
})
|
|
|
|
.AddTableEnsureManager(sp => new SqlServerTableEnsureManager<DefaultShardingDbContext>())
|
|
|
|
.End();
|
2021-12-09 15:46:18 +08:00
|
|
|
//services.AddShardingDbContext<DefaultShardingDbContext1>(
|
|
|
|
// (conn, o) =>
|
|
|
|
// o.UseSqlServer(conn).UseLoggerFactory(efLogger)
|
|
|
|
// ).Begin(o =>
|
|
|
|
// {
|
|
|
|
// o.CreateShardingTableOnStart = true;
|
|
|
|
// o.EnsureCreatedWithOutShardingTable = true;
|
|
|
|
// o.AutoTrackEntity = true;
|
|
|
|
// o.MaxQueryConnectionsLimit = Environment.ProcessorCount;
|
|
|
|
// o.ConnectionMode = ConnectionModeEnum.SYSTEM_AUTO;
|
|
|
|
// //if SysTest entity not exists in db and db is exists
|
|
|
|
// //o.AddEntityTryCreateTable<SysTest>(); // or `o.AddEntitiesTryCreateTable(typeof(SysTest));`
|
|
|
|
// })
|
|
|
|
// //.AddShardingQuery((conStr, builder) => builder.UseSqlServer(conStr).UseLoggerFactory(efLogger))//无需添加.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking) 并发查询系统会自动添加NoTracking
|
|
|
|
// .AddShardingTransaction((connection, builder) =>
|
|
|
|
// builder.UseSqlServer(connection).UseLoggerFactory(efLogger))
|
|
|
|
// .AddDefaultDataSource("A",
|
|
|
|
// "Data Source=localhost;Initial Catalog=ShardingCoreDBXA;Integrated Security=True;")
|
|
|
|
// .AddShardingTableRoute(o =>
|
|
|
|
// {
|
|
|
|
// }).End();
|
2021-11-13 11:13:47 +08:00
|
|
|
|
2021-11-25 14:00:01 +08:00
|
|
|
services.AddHealthChecks().AddDbContextCheck<DefaultShardingDbContext>();
|
2021-09-20 22:26:48 +08:00
|
|
|
//services.AddShardingDbContext<DefaultShardingDbContext, DefaultTableDbContext>(
|
|
|
|
// o => o.UseSqlServer("Data Source=localhost;Initial Catalog=ShardingCoreDB;Integrated Security=True;")
|
|
|
|
// , op =>
|
|
|
|
// {
|
|
|
|
// op.EnsureCreatedWithOutShardingTable = true;
|
|
|
|
// op.CreateShardingTableOnStart = true;
|
|
|
|
// op.UseShardingOptionsBuilder(
|
|
|
|
// (connection, builder) => builder.UseSqlServer(connection).UseLoggerFactory(efLogger),//使用dbconnection创建dbcontext支持事务
|
|
|
|
// (conStr,builder) => builder.UseSqlServer(conStr).UseLoggerFactory(efLogger).UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking)
|
|
|
|
// //.ReplaceService<IQueryTranslationPostprocessorFactory,SqlServer2008QueryTranslationPostprocessorFactory>()//支持sqlserver2008r2
|
|
|
|
// );//使用链接字符串创建dbcontext
|
|
|
|
// //op.UseReadWriteConfiguration(sp => new List<string>()
|
|
|
|
// //{
|
|
|
|
// // "Data Source=localhost;Initial Catalog=ShardingCoreDB1;Integrated Security=True;",
|
|
|
|
// // "Data Source=localhost;Initial Catalog=ShardingCoreDB2;Integrated Security=True;"
|
|
|
|
// //}, ReadStrategyEnum.Random);
|
|
|
|
// op.AddShardingTableRoute<SysUserModVirtualTableRoute>();
|
|
|
|
// op.AddShardingTableRoute<SysUserSalaryVirtualTableRoute>();
|
|
|
|
// });
|
2021-09-23 10:17:25 +08:00
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2021-12-12 13:40:32 +08:00
|
|
|
var startNew = Stopwatch.StartNew();
|
|
|
|
startNew.Start();
|
2021-01-26 14:39:56 +08:00
|
|
|
app.UseShardingCore();
|
2021-12-12 13:40:32 +08:00
|
|
|
startNew.Stop();
|
|
|
|
Console.WriteLine($"UseShardingCore:"+startNew.ElapsedMilliseconds+"ms");
|
2021-01-26 14:39:56 +08:00
|
|
|
app.UseRouting();
|
|
|
|
|
2021-03-08 14:59:32 +08:00
|
|
|
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
|
2021-11-09 14:13:00 +08:00
|
|
|
app.DbSeed();
|
2021-01-26 14:39:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|