修复定时任务在多张表的前提下进一张表会被创建
This commit is contained in:
parent
ede5c34286
commit
f01cf48003
|
@ -1,9 +1,9 @@
|
|||
:start
|
||||
::定义版本
|
||||
set EFCORE2=2.4.2.04
|
||||
set EFCORE3=3.4.2.04
|
||||
set EFCORE5=5.4.2.04
|
||||
set EFCORE6=6.4.2.04
|
||||
set EFCORE2=2.4.2.05
|
||||
set EFCORE3=3.4.2.05
|
||||
set EFCORE5=5.4.2.05
|
||||
set EFCORE6=6.4.2.05
|
||||
|
||||
::删除所有bin与obj下的文件
|
||||
@echo off
|
||||
|
|
|
@ -4,6 +4,11 @@ using System;
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Sample.SqlServer3x.Domain.Entities;
|
||||
using ShardingCore.Core.PhysicTables;
|
||||
using ShardingCore.Core.VirtualDatabase.VirtualTables;
|
||||
using ShardingCore.TableCreator;
|
||||
|
||||
namespace Sample.SqlServer3x.Controllers
|
||||
{
|
||||
|
@ -17,15 +22,71 @@ namespace Sample.SqlServer3x.Controllers
|
|||
};
|
||||
|
||||
private readonly ILogger<WeatherForecastController> _logger;
|
||||
private readonly DefaultDbContext _defaultDbContext;
|
||||
private readonly IShardingTableCreator<DefaultDbContext> _shardingTableCreator;
|
||||
private readonly IVirtualTableManager<DefaultDbContext> _virtualTableManager;
|
||||
|
||||
public WeatherForecastController(ILogger<WeatherForecastController> logger)
|
||||
public WeatherForecastController(ILogger<WeatherForecastController> logger,DefaultDbContext defaultDbContext, IShardingTableCreator<DefaultDbContext> shardingTableCreator, IVirtualTableManager<DefaultDbContext> virtualTableManager)
|
||||
{
|
||||
_logger = logger;
|
||||
_defaultDbContext = defaultDbContext;
|
||||
_shardingTableCreator = shardingTableCreator;
|
||||
_virtualTableManager = virtualTableManager;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public IEnumerable<WeatherForecast> Get()
|
||||
{
|
||||
Console.WriteLine("---------------开始-----------------");
|
||||
var s = DateTime.Now.ToString("HHmmss");
|
||||
|
||||
Task.Run(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
var virtualTable = _virtualTableManager.GetVirtualTable(typeof(SysUserMod));
|
||||
_virtualTableManager.AddPhysicTable(typeof(SysUserMod), new DefaultPhysicTable(virtualTable, s));
|
||||
_shardingTableCreator.CreateTable<SysUserMod>("A", s);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
}
|
||||
});
|
||||
Task.Run(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
var virtualTable = _virtualTableManager.GetVirtualTable(typeof(SysUserModAbc));
|
||||
_virtualTableManager.AddPhysicTable(typeof(SysUserModAbc), new DefaultPhysicTable(virtualTable, s));
|
||||
_shardingTableCreator.CreateTable<SysUserModAbc>("A", s);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
}
|
||||
});
|
||||
//try
|
||||
//{
|
||||
// var virtualTable = _virtualTableManager.GetVirtualTable(typeof(SysUserMod));
|
||||
// _virtualTableManager.AddPhysicTable(typeof(SysUserMod), new DefaultPhysicTable(virtualTable, s));
|
||||
// _shardingTableCreator.CreateTable<SysUserMod>("A", s);
|
||||
//}
|
||||
//catch (Exception e)
|
||||
//{
|
||||
// Console.WriteLine(e);
|
||||
//}
|
||||
//try
|
||||
//{
|
||||
// var virtualTable = _virtualTableManager.GetVirtualTable(typeof(SysUserModAbc));
|
||||
// _virtualTableManager.AddPhysicTable(typeof(SysUserModAbc), new DefaultPhysicTable(virtualTable, s));
|
||||
// _shardingTableCreator.CreateTable<SysUserModAbc>("A", s);
|
||||
//}
|
||||
//catch (Exception e)
|
||||
//{
|
||||
// Console.WriteLine(e);
|
||||
//}
|
||||
|
||||
var rng = new Random();
|
||||
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
|
||||
{
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Sample.SqlServer3x.Domain.Maps;
|
||||
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
|
||||
using ShardingCore.Sharding;
|
||||
using ShardingCore.Sharding.Abstractions;
|
||||
|
||||
namespace Sample.SqlServer3x
|
||||
|
@ -12,17 +14,18 @@ namespace Sample.SqlServer3x
|
|||
* @Ver: 1.0
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public class DefaultDbContext : DbContext, IShardingTableDbContext
|
||||
public class DefaultDbContext : AbstractShardingDbContext, IShardingTableDbContext
|
||||
{
|
||||
public DefaultDbContext(DbContextOptions<DefaultDbContext> options) : base(options)
|
||||
{
|
||||
|
||||
Console.WriteLine("DefaultDbContext ctor");
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.OnModelCreating(modelBuilder);
|
||||
modelBuilder.ApplyConfiguration(new SysUserModMap());
|
||||
modelBuilder.ApplyConfiguration(new SysUserModAbcMap());
|
||||
}
|
||||
|
||||
public IRouteTail RouteTail { get; set; }
|
||||
|
|
|
@ -13,7 +13,6 @@ namespace Sample.SqlServer3x.Domain.Entities
|
|||
/// <summary>
|
||||
/// 用户Id用于分表
|
||||
/// </summary>
|
||||
[ShardingTableKey]
|
||||
public string Id { get; set; }
|
||||
/// <summary>
|
||||
/// 用户名称
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
namespace Sample.SqlServer3x.Domain.Entities
|
||||
{
|
||||
public class SysUserModAbc
|
||||
{
|
||||
/// <summary>
|
||||
/// 用户Id用于分表
|
||||
/// </summary>
|
||||
public string Id { get; set; }
|
||||
/// <summary>
|
||||
/// 用户名称
|
||||
/// </summary>
|
||||
public string Name { get; set; }
|
||||
/// <summary>
|
||||
/// 用户姓名
|
||||
/// </summary>
|
||||
public int Age { get; set; }
|
||||
public int AgeGroup { get; set; }
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using Sample.SqlServer3x.Domain.Entities;
|
||||
|
||||
namespace Sample.SqlServer3x.Domain.Maps
|
||||
{
|
||||
public class SysUserModAbcMap:IEntityTypeConfiguration<SysUserModAbc>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<SysUserModAbc> builder)
|
||||
{
|
||||
builder.HasKey(o => o.Id);
|
||||
builder.Property(o => o.Id).IsRequired().HasMaxLength(128);
|
||||
builder.Property(o => o.Name).HasMaxLength(128);
|
||||
builder.ToTable(nameof(SysUserModAbc));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -4,6 +4,10 @@
|
|||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.23" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src3x\ShardingCore.3x\ShardingCore.3x.csproj" />
|
||||
</ItemGroup>
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
using Sample.SqlServer3x.Domain.Entities;
|
||||
using ShardingCore.Core.EntityMetadatas;
|
||||
using ShardingCore.VirtualRoutes.Mods;
|
||||
|
||||
namespace Sample.SqlServer3x.Shardings
|
||||
{
|
||||
/*
|
||||
* @Author: xjm
|
||||
* @Description:
|
||||
* @Date: Thursday, 14 January 2021 15:39:27
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public class SysUserModAbcVirtualTableRoute : AbstractSimpleShardingModKeyStringVirtualTableRoute<SysUserModAbc>
|
||||
{
|
||||
public SysUserModAbcVirtualTableRoute() : base(2,3)
|
||||
{
|
||||
}
|
||||
|
||||
public override void Configure(EntityMetadataTableBuilder<SysUserModAbc> builder)
|
||||
{
|
||||
builder.ShardingProperty(o => o.Id);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -18,7 +18,7 @@ namespace Sample.SqlServer3x.Shardings
|
|||
|
||||
public override void Configure(EntityMetadataTableBuilder<SysUserMod> builder)
|
||||
{
|
||||
|
||||
builder.ShardingProperty(o => o.Id);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -11,13 +11,19 @@ using System.Linq;
|
|||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Sample.SqlServer3x.Domain.Entities;
|
||||
using Sample.SqlServer3x.Shardings;
|
||||
using ShardingCore;
|
||||
using ShardingCore.Bootstrapers;
|
||||
using ShardingCore.TableExists;
|
||||
|
||||
namespace Sample.SqlServer3x
|
||||
{
|
||||
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;
|
||||
|
@ -42,6 +48,33 @@ namespace Sample.SqlServer3x
|
|||
// });
|
||||
// services.AddDbContext<DefaultDbContext>(o => o.UseSqlServer("Data Source=localhost;Initial Catalog=ShardingCoreDB3x;Integrated Security=True")
|
||||
// .UseShardingSqlServerUpdateSqlGenerator());
|
||||
//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>()
|
||||
.AddEntityConfig(o =>
|
||||
{
|
||||
o.CreateShardingTableOnStart = true;
|
||||
o.EnsureCreatedWithOutShardingTable = true;
|
||||
o.AddShardingTableRoute<SysUserModVirtualTableRoute>();
|
||||
o.AddShardingTableRoute<SysUserModAbcVirtualTableRoute>();
|
||||
})
|
||||
.AddConfig(op =>
|
||||
{
|
||||
op.ConfigId = "c1";
|
||||
op.MaxQueryConnectionsLimit = 5;
|
||||
op.UseShardingQuery((conStr, builder) =>
|
||||
{
|
||||
builder.UseSqlServer(conStr).UseLoggerFactory(efLogger);
|
||||
});
|
||||
op.UseShardingTransaction((conn, builder) =>
|
||||
{
|
||||
builder.UseSqlServer(conn).UseLoggerFactory(efLogger);
|
||||
});
|
||||
op.ReplaceTableEnsureManager(sp => new SqlServerTableEnsureManager<DefaultDbContext>());
|
||||
op.AddDefaultDataSource("A",
|
||||
"Data Source=localhost;Initial Catalog=ShardingCoreCreate;Integrated Security=True;"
|
||||
);
|
||||
}).EnsureConfig();
|
||||
}
|
||||
|
||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||
|
@ -63,7 +96,7 @@ namespace Sample.SqlServer3x
|
|||
endpoints.MapControllers();
|
||||
});
|
||||
|
||||
InitData(app).GetAwaiter().GetResult();
|
||||
//InitData(app).GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
@ -29,6 +29,9 @@ namespace ShardingCore.Extensions
|
|||
/// <param name="dbContext"></param>
|
||||
public static void RemoveDbContextRelationModelThatIsShardingTable(this DbContext dbContext)
|
||||
{
|
||||
#if !EFCORE2&&!EFCORE3&&!EFCORE5&&!EFCORE6
|
||||
throw new NotImplementedException();
|
||||
#endif
|
||||
#if EFCORE6
|
||||
|
||||
var contextModel = dbContext.GetService<IDesignTimeModel>().Model; ;
|
||||
|
@ -102,6 +105,9 @@ namespace ShardingCore.Extensions
|
|||
/// <param name="dbContext"></param>
|
||||
public static void RemoveDbContextAllRelationModelThatIsNoSharding(this DbContext dbContext)
|
||||
{
|
||||
#if !EFCORE2&&!EFCORE3&&!EFCORE5&&!EFCORE6
|
||||
throw new NotImplementedException();
|
||||
#endif
|
||||
#if EFCORE6
|
||||
|
||||
var contextModel = dbContext.GetService<IDesignTimeModel>().Model; ;
|
||||
|
@ -146,6 +152,9 @@ namespace ShardingCore.Extensions
|
|||
/// <param name="dbContext"></param>
|
||||
public static void RemoveDbContextAllRelationModel(this DbContext dbContext)
|
||||
{
|
||||
#if !EFCORE2&&!EFCORE3&&!EFCORE5&&!EFCORE6
|
||||
throw new NotImplementedException();
|
||||
#endif
|
||||
#if EFCORE6
|
||||
|
||||
var contextModel = dbContext.GetService<IDesignTimeModel>().Model; ;
|
||||
|
@ -178,6 +187,9 @@ namespace ShardingCore.Extensions
|
|||
public static void RemoveDbContextRelationModelSaveOnlyThatIsNamedType(this DbContext dbContext,
|
||||
Type shardingType)
|
||||
{
|
||||
#if !EFCORE2&&!EFCORE3&&!EFCORE5&&!EFCORE6
|
||||
throw new NotImplementedException();
|
||||
#endif
|
||||
#if EFCORE2 ||EFCORE3 ||EFCORE5
|
||||
|
||||
var contextModel = dbContext.Model as Model;
|
||||
|
@ -228,6 +240,9 @@ namespace ShardingCore.Extensions
|
|||
/// <param name="dbContext"></param>
|
||||
public static void RemoveModelCache(this DbContext dbContext)
|
||||
{
|
||||
#if !EFCORE2&&!EFCORE3&&!EFCORE5&&!EFCORE6
|
||||
throw new NotImplementedException();
|
||||
#endif
|
||||
#if EFCORE6
|
||||
var shardingModelSource = dbContext.GetService<IModelSource>() as IShardingModelSource;
|
||||
var modelCacheKeyFactory = shardingModelSource.GetModelCacheKeyFactory();
|
||||
|
|
|
@ -19,16 +19,18 @@ namespace ShardingCore.TableCreator
|
|||
/// </summary>
|
||||
/// <param name="dataSourceName"></param>
|
||||
/// <param name="tail"></param>
|
||||
/// <param name="timeOut"></param>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
void CreateTable<T>(string dataSourceName, string tail) where T : class;
|
||||
void CreateTable<T>(string dataSourceName, string tail,int timeOut=6000) where T : class;
|
||||
/// <summary>
|
||||
/// 创建表
|
||||
/// </summary>
|
||||
/// <param name="dataSourceName"></param>
|
||||
/// <param name="shardingEntityType"></param>
|
||||
/// <param name="tail"></param>
|
||||
/// <param name="timeOut"></param>
|
||||
/// <exception cref="ShardingCreateException"></exception>
|
||||
void CreateTable(string dataSourceName, Type shardingEntityType, string tail);
|
||||
void CreateTable(string dataSourceName, Type shardingEntityType, string tail, int timeOut = 6000);
|
||||
}
|
||||
public interface IShardingTableCreator<TShardingDbContext>: IShardingTableCreator where TShardingDbContext : DbContext, IShardingDbContext
|
||||
{
|
||||
|
|
|
@ -25,6 +25,7 @@ namespace ShardingCore.TableCreator
|
|||
private readonly IServiceProvider _serviceProvider;
|
||||
private readonly IShardingEntityConfigOptions<TShardingDbContext> _entityConfigOptions;
|
||||
private readonly IRouteTailFactory _routeTailFactory;
|
||||
private readonly object _lock = new object();
|
||||
|
||||
public ShardingTableCreator(ILogger<ShardingTableCreator<TShardingDbContext>> logger, IServiceProvider serviceProvider, IShardingEntityConfigOptions<TShardingDbContext> entityConfigOptions, IRouteTailFactory routeTailFactory)
|
||||
{
|
||||
|
@ -34,9 +35,9 @@ namespace ShardingCore.TableCreator
|
|||
_routeTailFactory = routeTailFactory;
|
||||
}
|
||||
|
||||
public void CreateTable<T>(string dataSourceName, string tail) where T : class
|
||||
public void CreateTable<T>(string dataSourceName, string tail, int timeOut = 6000) where T : class
|
||||
{
|
||||
CreateTable(dataSourceName, typeof(T), tail);
|
||||
CreateTable(dataSourceName, typeof(T), tail, timeOut);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -45,7 +46,14 @@ namespace ShardingCore.TableCreator
|
|||
/// <param name="dataSourceName"></param>
|
||||
/// <param name="shardingEntityType"></param>
|
||||
/// <param name="tail"></param>
|
||||
public void CreateTable(string dataSourceName, Type shardingEntityType, string tail)
|
||||
public void CreateTable(string dataSourceName, Type shardingEntityType, string tail, int timeOut = 6000)
|
||||
{
|
||||
var acquire = Monitor.TryEnter(_lock, TimeSpan.FromMilliseconds(timeOut));
|
||||
if (!acquire)
|
||||
{
|
||||
throw new ShardingCoreException("CreateTable cant get _lock");
|
||||
}
|
||||
try
|
||||
{
|
||||
using (var serviceScope = _serviceProvider.CreateScope())
|
||||
{
|
||||
|
@ -53,16 +61,7 @@ namespace ShardingCore.TableCreator
|
|||
var shardingDbContext = (IShardingDbContext)dbContext;
|
||||
var context = shardingDbContext.GetDbContext(dataSourceName, false, _routeTailFactory.Create(tail));
|
||||
|
||||
var modelCacheSyncObject = context.GetModelCacheSyncObject();
|
||||
|
||||
var acquire = Monitor.TryEnter(modelCacheSyncObject,TimeSpan.FromSeconds(3));
|
||||
if (!acquire)
|
||||
{
|
||||
throw new ShardingCoreException("cant get modelCacheSyncObject lock");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
context.RemoveDbContextRelationModelSaveOnlyThatIsNamedType(shardingEntityType);
|
||||
var databaseCreator = context.Database.GetService<IDatabaseCreator>() as RelationalDatabaseCreator;
|
||||
try
|
||||
|
@ -82,13 +81,13 @@ namespace ShardingCore.TableCreator
|
|||
{
|
||||
context.RemoveModelCache();
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
Monitor.Exit(modelCacheSyncObject);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
Monitor.Exit(_lock);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue