diff --git a/samples/Sample.AutoCreateIfPresent/Controllers/TestController.cs b/samples/Sample.AutoCreateIfPresent/Controllers/TestController.cs new file mode 100644 index 00000000..2679af70 --- /dev/null +++ b/samples/Sample.AutoCreateIfPresent/Controllers/TestController.cs @@ -0,0 +1,58 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; + +namespace Sample.AutoCreateIfPresent.Controllers; + +[ApiController] +[Route("[controller]/[action]")] +public class TestController : ControllerBase +{ + private readonly ILogger _logger; + private readonly DefaultDbContext _defaultDbContext; + + public TestController(ILogger logger,DefaultDbContext defaultDbContext) + { + _logger = logger; + _defaultDbContext = defaultDbContext; + } + + public IActionResult HelloWorld() + { + return Ok("hello world"); + } + + public async Task Query() + { + var list =await _defaultDbContext.Set().ToListAsync(); + return Ok(list); + } + public async Task Insert() + { + var orderByHour = new OrderByHour(); + orderByHour.Id = Guid.NewGuid().ToString("n"); + orderByHour.Name=$"Name:"+ Guid.NewGuid().ToString("n"); + var dateTime = DateTime.Now; + orderByHour.CreateTime = dateTime.AddHours(new Random().Next(1, 20)); + await _defaultDbContext.AddAsync(orderByHour); + await _defaultDbContext.SaveChangesAsync(); + return Ok(); + } + public async Task Query1() + { + var list = await _defaultDbContext.Set().ToListAsync(); + return Ok(list); + } + public async Task Insert1() + { + var list = new List(){"A","B","C","D","E", "F", "G" }; + var orderByHour = new AreaDevice(); + orderByHour.Id = Guid.NewGuid().ToString("n"); + orderByHour.Area = list[new Random().Next(0, list.Count)]; + var dateTime = DateTime.Now; + orderByHour.CreateTime = dateTime.AddHours(new Random().Next(1, 20)); + await _defaultDbContext.AddAsync(orderByHour); + await _defaultDbContext.SaveChangesAsync(); + return Ok(); + } + +} \ No newline at end of file diff --git a/samples/Sample.AutoCreateIfPresent/Controllers/WeatherForecastController.cs b/samples/Sample.AutoCreateIfPresent/Controllers/WeatherForecastController.cs index 0f61f0fe..5f1784ec 100644 --- a/samples/Sample.AutoCreateIfPresent/Controllers/WeatherForecastController.cs +++ b/samples/Sample.AutoCreateIfPresent/Controllers/WeatherForecastController.cs @@ -1,3 +1,4 @@ +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; diff --git a/samples/Sample.AutoCreateIfPresent/OrderByHourRoute.cs b/samples/Sample.AutoCreateIfPresent/OrderByHourRoute.cs index 1b2d5f0b..ccf21bb3 100644 --- a/samples/Sample.AutoCreateIfPresent/OrderByHourRoute.cs +++ b/samples/Sample.AutoCreateIfPresent/OrderByHourRoute.cs @@ -14,6 +14,7 @@ using ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions; using ShardingCore.Exceptions; using ShardingCore.Extensions; using ShardingCore.TableCreator; +using ShardingCore.TableExists; /* * @Author: xjm @@ -43,6 +44,11 @@ namespace Sample.AutoCreateIfPresent _shardingTableCreator = shardingTableCreator; } + public override string ShardingKeyToTail(object shardingKey) + { + var dateTime = (DateTime)shardingKey; + return ShardingKeyFormat(dateTime); + } private string ShardingKeyFormat(DateTime dateTime) { var tail = $"{dateTime:yyyyMMddHH}"; @@ -50,12 +56,6 @@ namespace Sample.AutoCreateIfPresent return tail; } - public override string ShardingKeyToTail(object shardingKey) - { - var dateTime = (DateTime)shardingKey; - return ShardingKeyFormat(dateTime); - } - /// /// 如果你是非mysql数据库请自行实现这个方法返回当前类在数据库已经存在的后缀 /// 仅启动时调用 @@ -134,6 +134,7 @@ namespace Sample.AutoCreateIfPresent if (!_tails.TryGetValue(shardingKeyToTail,out var _)) { var virtualTable = _virtualTableManager.GetVirtualTable(typeof(OrderByHour)); +//必须先执行AddPhysicTable在进行CreateTable _virtualTableManager.AddPhysicTable(virtualTable, new DefaultPhysicTable(virtualTable, shardingKeyToTail)); try { diff --git a/samples/Sample.AutoCreateIfPresent/Program.cs b/samples/Sample.AutoCreateIfPresent/Program.cs index daf29079..61555ac9 100644 --- a/samples/Sample.AutoCreateIfPresent/Program.cs +++ b/samples/Sample.AutoCreateIfPresent/Program.cs @@ -16,8 +16,8 @@ var builder = WebApplication.CreateBuilder(args); builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle -builder.Services.AddEndpointsApiExplorer(); -builder.Services.AddSwaggerGen(); +// builder.Services.AddEndpointsApiExplorer(); +// builder.Services.AddSwaggerGen(); builder.Services.AddShardingDbContext() .AddEntityConfig(o => { @@ -30,7 +30,7 @@ builder.Services.AddShardingDbContext() .AddConfig(o => { o.ConfigId = "c1"; - o.AddDefaultDataSource("ds0", "server=127.0.0.1;port=3306;database=shardingTest;userid=root;password=L6yBtV6qNENrwBy7;"); + o.AddDefaultDataSource("ds0", "server=127.0.0.1;port=3306;database=shardingTest;userid=root;password=root;"); o.UseShardingQuery((conn, b) => { b.UseMySql(conn, new MySqlServerVersion(new Version())).UseLoggerFactory(efLogger); @@ -46,8 +46,8 @@ var app = builder.Build(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { - app.UseSwagger(); - app.UseSwaggerUI(); + // app.UseSwagger(); + // app.UseSwaggerUI(); } app.Services.GetRequiredService().Start(); diff --git a/src/ShardingCore/Core/VirtualRoutes/TableRoutes/IVirtualTableRoute.cs b/src/ShardingCore/Core/VirtualRoutes/TableRoutes/IVirtualTableRoute.cs index 31e14904..b3ce6004 100644 --- a/src/ShardingCore/Core/VirtualRoutes/TableRoutes/IVirtualTableRoute.cs +++ b/src/ShardingCore/Core/VirtualRoutes/TableRoutes/IVirtualTableRoute.cs @@ -39,7 +39,7 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes /// IPhysicTable RouteWithValue(List allPhysicTables, object shardingKey); /// - /// 获取所有的目前数据库存在的尾巴 + /// 获取所有的目前数据库存在的尾巴,仅启动时调用 /// get all tails in the db /// /// diff --git a/src/ShardingCore/VirtualRoutes/Abstractions/AbstractShardingAutoCreateOperatorVirtualTableRoute.cs b/src/ShardingCore/VirtualRoutes/Abstractions/AbstractShardingAutoCreateOperatorVirtualTableRoute.cs index 516e938a..c4fcb955 100644 --- a/src/ShardingCore/VirtualRoutes/Abstractions/AbstractShardingAutoCreateOperatorVirtualTableRoute.cs +++ b/src/ShardingCore/VirtualRoutes/Abstractions/AbstractShardingAutoCreateOperatorVirtualTableRoute.cs @@ -77,6 +77,7 @@ namespace ShardingCore.VirtualRoutes.Abstractions var allVirtualDataSources = virtualDataSourceManager.GetAllVirtualDataSources(); var now = DateTime.Now.AddMinutes(IncrementMinutes); var tail = ConvertNowToTail(now); +//必须先执行AddPhysicTable在进行CreateTable virtualTableManager.AddPhysicTable(virtualTable, new DefaultPhysicTable(virtualTable, tail)); foreach (var virtualDataSource in allVirtualDataSources) {