2021-03-31 15:55:27 +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;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
2021-08-15 07:32:50 +08:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2022-04-03 16:21:51 +08:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
2021-03-31 15:55:27 +08:00
|
|
|
|
using Sample.SqlServer3x.Domain.Entities;
|
2022-04-01 12:40:15 +08:00
|
|
|
|
using Sample.SqlServer3x.Shardings;
|
2021-03-31 15:55:27 +08:00
|
|
|
|
using ShardingCore;
|
2022-06-17 10:57:25 +08:00
|
|
|
|
using ShardingCore.Bootstrappers;
|
2022-04-03 16:21:51 +08:00
|
|
|
|
using ShardingCore.Core.DbContextCreator;
|
2022-04-01 12:40:15 +08:00
|
|
|
|
using ShardingCore.TableExists;
|
2022-07-03 16:52:03 +08:00
|
|
|
|
using ShardingCore.TableExists.Abstractions;
|
2021-03-31 15:55:27 +08:00
|
|
|
|
|
|
|
|
|
namespace Sample.SqlServer3x
|
|
|
|
|
{
|
|
|
|
|
public class Startup
|
|
|
|
|
{
|
2022-04-01 12:40:15 +08:00
|
|
|
|
public static readonly ILoggerFactory efLogger = LoggerFactory.Create(builder =>
|
|
|
|
|
{
|
|
|
|
|
builder.AddFilter((category, level) => category == DbLoggerCategory.Database.Command.Name && level == LogLevel.Information).AddConsole();
|
|
|
|
|
});
|
2021-03-31 15:55:27 +08:00
|
|
|
|
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();
|
2021-08-20 23:10:34 +08:00
|
|
|
|
// services.AddShardingSqlServer(o =>
|
|
|
|
|
// {
|
|
|
|
|
// o.EnsureCreatedWithOutShardingTable = true;
|
|
|
|
|
// o.CreateShardingTableOnStart = true;
|
|
|
|
|
// o.UseShardingDbContext<DefaultDbContext>( dbConfig =>
|
|
|
|
|
// {
|
|
|
|
|
// dbConfig.AddShardingTableRoute<SysUserModVirtualTableRoute>();
|
|
|
|
|
// });
|
|
|
|
|
// //o.AddDataSourceVirtualRoute<>();
|
|
|
|
|
//
|
|
|
|
|
// });
|
|
|
|
|
// services.AddDbContext<DefaultDbContext>(o => o.UseSqlServer("Data Source=localhost;Initial Catalog=ShardingCoreDB3x;Integrated Security=True")
|
|
|
|
|
// .UseShardingSqlServerUpdateSqlGenerator());
|
2022-04-01 12:40:15 +08:00
|
|
|
|
//services.AddDbContext<DefaultDbContext>(op=>op.UseSqlServer("Data Source=localhost;Initial Catalog=ShardingCoreCreate;Integrated Security=True;"));
|
|
|
|
|
//services.AddScoped<DbContext,DefaultDbContext>(s=>s.GetService<DefaultDbContext>());
|
|
|
|
|
services.AddShardingDbContext<DefaultDbContext>()
|
2022-07-03 16:52:03 +08:00
|
|
|
|
.UseRouteConfig(o =>
|
2022-04-01 12:40:15 +08:00
|
|
|
|
{
|
|
|
|
|
o.AddShardingTableRoute<SysUserModVirtualTableRoute>();
|
|
|
|
|
o.AddShardingTableRoute<SysUserModAbcVirtualTableRoute>();
|
|
|
|
|
})
|
2022-07-03 16:52:03 +08:00
|
|
|
|
.UseConfig(op =>
|
2022-04-01 12:40:15 +08:00
|
|
|
|
{
|
|
|
|
|
op.MaxQueryConnectionsLimit = 5;
|
|
|
|
|
op.UseShardingQuery((conStr, builder) =>
|
|
|
|
|
{
|
|
|
|
|
builder.UseSqlServer(conStr).UseLoggerFactory(efLogger);
|
|
|
|
|
});
|
|
|
|
|
op.UseShardingTransaction((conn, builder) =>
|
|
|
|
|
{
|
|
|
|
|
builder.UseSqlServer(conn).UseLoggerFactory(efLogger);
|
|
|
|
|
});
|
|
|
|
|
op.AddDefaultDataSource("A",
|
|
|
|
|
"Data Source=localhost;Initial Catalog=ShardingCoreCreate;Integrated Security=True;"
|
|
|
|
|
);
|
2022-07-03 16:52:03 +08:00
|
|
|
|
})
|
|
|
|
|
.ReplaceService<ITableEnsureManager,SqlServerTableEnsureManager>(ServiceLifetime.Singleton)
|
|
|
|
|
.ReplaceService<IDbContextCreator,CustomerDbContextCreator>(ServiceLifetime.Singleton)
|
|
|
|
|
.AddShardingCore();
|
2022-04-03 16:21:51 +08:00
|
|
|
|
services.AddScoped<IScopedService, ScopedService>();
|
|
|
|
|
services.Replace(
|
2022-07-03 16:52:03 +08:00
|
|
|
|
ServiceDescriptor.Singleton<IDbContextCreator, CustomerDbContextCreator>());
|
2021-03-31 15:55:27 +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-07-03 16:52:03 +08:00
|
|
|
|
app.ApplicationServices.UseAutoTryCompensateTable();
|
2021-03-31 15:55:27 +08:00
|
|
|
|
app.UseRouting();
|
|
|
|
|
|
|
|
|
|
app.UseAuthorization();
|
|
|
|
|
|
|
|
|
|
app.UseEndpoints(endpoints =>
|
|
|
|
|
{
|
|
|
|
|
endpoints.MapControllers();
|
|
|
|
|
});
|
|
|
|
|
|
2022-04-01 12:40:15 +08:00
|
|
|
|
//InitData(app).GetAwaiter().GetResult();
|
2021-03-31 15:55:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2021-08-15 07:32:50 +08:00
|
|
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2021-03-31 15:55:27 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="serviceProvider"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
private async Task InitData(IApplicationBuilder app)
|
|
|
|
|
{
|
|
|
|
|
var serviceProvider = app.ApplicationServices;
|
|
|
|
|
using (var scope = serviceProvider.CreateScope())
|
|
|
|
|
{
|
2021-08-15 07:32:50 +08:00
|
|
|
|
var virtualDbContext = scope.ServiceProvider.GetService<DefaultDbContext>();
|
2021-08-20 23:10:34 +08:00
|
|
|
|
if (!await virtualDbContext.Set<SysUserMod>().AnyAsync(o => true))
|
2021-03-31 15:55:27 +08:00
|
|
|
|
{
|
|
|
|
|
var ids = Enumerable.Range(1, 1000);
|
|
|
|
|
var userMods = new List<SysUserMod>();
|
|
|
|
|
var beginTime = new DateTime(2020, 1, 1);
|
|
|
|
|
var endTime = new DateTime(2021, 12, 1);
|
|
|
|
|
foreach (var id in ids)
|
|
|
|
|
{
|
|
|
|
|
userMods.Add(new SysUserMod()
|
|
|
|
|
{
|
|
|
|
|
Id = id.ToString(),
|
|
|
|
|
Age = id,
|
|
|
|
|
Name = $"name_{id}",
|
|
|
|
|
AgeGroup = Math.Abs(id % 10)
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-15 07:32:50 +08:00
|
|
|
|
await virtualDbContext.AddRangeAsync(userMods);
|
2021-03-31 15:55:27 +08:00
|
|
|
|
|
|
|
|
|
await virtualDbContext.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|