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-08-27 09:57:40 +08:00
|
|
|
using Microsoft.EntityFrameworkCore.Query;
|
|
|
|
using Microsoft.EntityFrameworkCore.SqlServer.Query.Internal;
|
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-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-08-21 04:46:04 +08:00
|
|
|
services.AddShardingDbContext<DefaultShardingDbContext, DefaultTableDbContext>(
|
2021-08-26 19:26:56 +08:00
|
|
|
o => o.UseSqlServer("Data Source=localhost;Initial Catalog=ShardingCoreDB;Integrated Security=True;")
|
2021-08-21 08:51:27 +08:00
|
|
|
, op =>
|
|
|
|
{
|
2021-08-27 10:06:29 +08:00
|
|
|
op.EnsureCreatedWithOutShardingTable = false;
|
|
|
|
op.CreateShardingTableOnStart = false;
|
2021-08-23 08:18:17 +08:00
|
|
|
op.UseShardingOptionsBuilder(
|
|
|
|
(connection, builder) => builder.UseSqlServer(connection).UseLoggerFactory(efLogger),//使用dbconnection创建dbcontext支持事务
|
2021-08-27 10:06:29 +08:00
|
|
|
(conStr,builder) => builder.UseSqlServer(conStr).UseLoggerFactory(efLogger)
|
2021-08-27 09:57:40 +08:00
|
|
|
//.ReplaceService<IQueryTranslationPostprocessorFactory,SqlServer2008QueryTranslationPostprocessorFactory>()//支持sqlserver2008r2
|
2021-08-27 10:06:29 +08:00
|
|
|
);//使用链接字符串创建dbcontext
|
2021-08-21 08:51:27 +08:00
|
|
|
op.AddShardingTableRoute<SysUserModVirtualTableRoute>();
|
|
|
|
});
|
2021-08-27 13:31:50 +08:00
|
|
|
|
2021-08-21 08:51:27 +08:00
|
|
|
////不支持MARS不支持追踪的
|
|
|
|
//services.AddShardingDbContext<DefaultShardingDbContext, DefaultTableDbContext>(o => o.UseSqlServer("Data Source=localhost;Initial Catalog=ShardingCoreDBxx2;Integrated Security=True;")
|
|
|
|
// , op =>
|
|
|
|
// {
|
|
|
|
// op.EnsureCreatedWithOutShardingTable = true;
|
|
|
|
// op.CreateShardingTableOnStart = true;
|
|
|
|
// //不支持mars额外加一条字符串的
|
|
|
|
// op.UseShardingOptionsBuilder(
|
|
|
|
// (connection, builder) => builder.UseSqlServer(connection).UseLoggerFactory(efLogger),
|
|
|
|
// (connString, builder) => builder.UseSqlServer(connString).UseLoggerFactory(efLogger));
|
|
|
|
|
|
|
|
// op.AddShardingTableRoute<SysUserModVirtualTableRoute>();
|
|
|
|
// });
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
app.UseShardingCore();
|
|
|
|
|
|
|
|
app.UseRouting();
|
|
|
|
|
2021-03-08 14:59:32 +08:00
|
|
|
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
|
2021-08-20 13:50:49 +08:00
|
|
|
app.DbSeed();
|
2021-01-26 14:39:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|