86 lines
3.2 KiB
C#
86 lines
3.2 KiB
C#
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.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Sample.SqlServerShardingDataSource.DbContexts;
|
|
using Sample.SqlServerShardingDataSource.Shardings;
|
|
using ShardingCore;
|
|
|
|
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();
|
|
|
|
|
|
services.AddShardingDbContext<DefaultShardingDbContext, DefaultDbContext>(
|
|
o =>
|
|
o.UseSqlServer("Data Source=localhost;Initial Catalog=ShardingCoreDBxx0;Integrated Security=True;")
|
|
).Begin(o =>
|
|
{
|
|
o.CreateShardingTableOnStart = true;
|
|
o.EnsureCreatedWithOutShardingTable = true;
|
|
})
|
|
.AddShardingQuery((conStr, builder) => builder.UseSqlServer(conStr).UseLoggerFactory(efLogger)
|
|
.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking))
|
|
.AddShardingTransaction((connection, builder) =>
|
|
builder.UseSqlServer(connection).UseLoggerFactory(efLogger))
|
|
.AddDefaultDataSource("ds0","Data Source=localhost;Initial Catalog=ShardingCoreDBxx0;Integrated Security=True;")
|
|
.AddShardingDataSource(sp =>
|
|
{
|
|
return new Dictionary<string, string>()
|
|
{
|
|
{"ds1", "Data Source=localhost;Initial Catalog=ShardingCoreDBxx1;Integrated Security=True;"},
|
|
{"ds2", "Data Source=localhost;Initial Catalog=ShardingCoreDBxx2;Integrated Security=True;"},
|
|
};
|
|
}).AddShardingDataSourceRoute(o =>
|
|
{
|
|
o.AddShardingDatabaseRoute<SysUserModVirtualDataSourceRoute>();
|
|
}).End();
|
|
}
|
|
|
|
// 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();
|
|
|
|
app.UseAuthorization();
|
|
|
|
app.UseEndpoints(endpoints =>
|
|
{
|
|
endpoints.MapControllers();
|
|
});
|
|
app.DbSeed();
|
|
}
|
|
}
|
|
}
|