[#215] 并且修复补偿表+HasData的bug 发布6.8.0.4
This commit is contained in:
parent
c34b3a816d
commit
339b543477
|
@ -1,6 +1,6 @@
|
|||
:start
|
||||
::定义版本
|
||||
set SHARDINGCORE=6.8.0.3
|
||||
set SHARDINGCORE=6.8.0.4
|
||||
|
||||
::删除所有bin与obj下的文件
|
||||
@echo off
|
||||
|
|
|
@ -87,10 +87,10 @@ namespace Sample.MySql.Controllers
|
|||
// using (var tran = _defaultTableDbContext.Database.BeginTransaction())
|
||||
// {
|
||||
|
||||
SysUserMod? resultX123 = await _defaultTableDbContext.Set<SysUserMod>()
|
||||
.Where(o => o.Id == "2").FirstOrDefaultAsync();
|
||||
_defaultTableDbContext.Update(resultX123);
|
||||
_defaultTableDbContext.SaveChanges();
|
||||
// SysUserMod? resultX123 = await _defaultTableDbContext.Set<SysUserMod>()
|
||||
// .Where(o => o.Id == "2").FirstOrDefaultAsync();
|
||||
// _defaultTableDbContext.Update(resultX123);
|
||||
// _defaultTableDbContext.SaveChanges();
|
||||
var resultX1 = await _defaultTableDbContext.Set<SysUserMod>()
|
||||
.Where(o => o.Id == "2" || o.Id == "3").GroupBy(o => new { o.Id,o.Name })
|
||||
.Select(o => new
|
||||
|
|
|
@ -20,14 +20,15 @@ namespace Sample.MySql.DbContexts
|
|||
//Database.SetCommandTimeout(30000);
|
||||
}
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
base.OnConfiguring(optionsBuilder);
|
||||
optionsBuilder.UseLazyLoadingProxies();
|
||||
}
|
||||
// protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
// {
|
||||
// base.OnConfiguring(optionsBuilder);
|
||||
// optionsBuilder.UseLazyLoadingProxies();
|
||||
// }
|
||||
|
||||
private readonly MethodInfo? _configureGlobalFiltersMethodInfo =
|
||||
typeof(DefaultShardingDbContext).GetMethod(nameof(ConfigureGlobalFilters), BindingFlags.Instance | BindingFlags.NonPublic);
|
||||
typeof(DefaultShardingDbContext).GetMethod(nameof(ConfigureGlobalFilters),
|
||||
BindingFlags.Instance | BindingFlags.NonPublic);
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
|
@ -55,15 +56,16 @@ namespace Sample.MySql.DbContexts
|
|||
if (typeof(TEntity) == typeof(SysTest))
|
||||
{
|
||||
expression = e => ((IUser)e).UserId == "123";
|
||||
}
|
||||
|
||||
} if (typeof(TEntity) == typeof(SysUserMod))
|
||||
if (typeof(TEntity) == typeof(SysUserMod))
|
||||
{
|
||||
expression = e => ((IAge)e).Age == 99;
|
||||
|
||||
}
|
||||
|
||||
return expression;
|
||||
}
|
||||
|
||||
public IRouteTail RouteTail { get; set; }
|
||||
}
|
||||
}
|
|
@ -9,14 +9,14 @@ public class SysUserModVirtualDataSourceRoute:AbstractShardingOperatorVirtualDat
|
|||
{
|
||||
public override string ShardingKeyToDataSourceName(object shardingKey)
|
||||
{
|
||||
return $"{shardingKey}";
|
||||
return $"{((string)shardingKey=="ds1"?"ds1":"ds2")}";
|
||||
}
|
||||
|
||||
public override List<string> GetAllDataSourceNames()
|
||||
{
|
||||
return new List<string>()
|
||||
{
|
||||
"ds0", "ds1", "ds2"
|
||||
"ds1", "ds2"
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -103,7 +103,7 @@ namespace Sample.MySql
|
|||
o.AddShardingDataSourceRoute<SysUserModVirtualDataSourceRoute>();
|
||||
}).UseConfig(o =>
|
||||
{
|
||||
o.UseEntityFrameworkCoreProxies = true;
|
||||
// o.UseEntityFrameworkCoreProxies = true;
|
||||
o.ThrowIfQueryRouteNotMatch = false;
|
||||
o.AutoUseWriteConnectionStringAfterWriteDb = true;
|
||||
o.UseShardingQuery((conStr, builder) =>
|
||||
|
@ -185,14 +185,14 @@ namespace Sample.MySql
|
|||
// var shardingRuntimeContext = app.ApplicationServices.GetRequiredService<IShardingRuntimeContext>();
|
||||
// var entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager();
|
||||
// var entityMetadata = entityMetadataManager.TryGet<SysUserMod>();
|
||||
using (var scope = app.ApplicationServices.CreateScope())
|
||||
{
|
||||
var defaultShardingDbContext = scope.ServiceProvider.GetService<DefaultShardingDbContext>();
|
||||
// if (defaultShardingDbContext.Database.GetPendingMigrations().Any())
|
||||
{
|
||||
defaultShardingDbContext.Database.Migrate();
|
||||
}
|
||||
}
|
||||
// using (var scope = app.ApplicationServices.CreateScope())
|
||||
// {
|
||||
// var defaultShardingDbContext = scope.ServiceProvider.GetService<DefaultShardingDbContext>();
|
||||
// // if (defaultShardingDbContext.Database.GetPendingMigrations().Any())
|
||||
// {
|
||||
// defaultShardingDbContext.Database.Migrate();
|
||||
// }
|
||||
// }
|
||||
app.ApplicationServices.UseAutoTryCompensateTable();
|
||||
// using (var scope = app.ApplicationServices.CreateScope())
|
||||
// {
|
||||
|
|
|
@ -16,7 +16,6 @@ using ShardingCore.Core.VirtualRoutes.Abstractions;
|
|||
using ShardingCore.Core.VirtualRoutes.TableRoutes;
|
||||
using ShardingCore.Exceptions;
|
||||
using ShardingCore.Extensions;
|
||||
|
||||
using ShardingCore.Sharding.Abstractions;
|
||||
using ShardingCore.TableExists.Abstractions;
|
||||
|
||||
|
@ -77,33 +76,27 @@ namespace ShardingCore.DynamicDataSources
|
|||
|
||||
if (createTable)
|
||||
{
|
||||
var existTables = _tableEnsureManager.GetExistTables((IShardingDbContext)shellDbContext,dataSourceName);
|
||||
var existTables =
|
||||
_tableEnsureManager.GetExistTables((IShardingDbContext)shellDbContext, dataSourceName);
|
||||
var allShardingEntities = _entityMetadataManager.GetAllShardingEntities();
|
||||
foreach (var entityType in allShardingEntities)
|
||||
{
|
||||
//如果是默认数据源
|
||||
if (_virtualDataSource.IsDefault(dataSourceName))
|
||||
{
|
||||
if (_entityMetadataManager.IsShardingTable(entityType))
|
||||
{
|
||||
var virtualTableRoute = _tableRouteManager.GetRoute(entityType);
|
||||
CreateDataTable(dataSourceName, virtualTableRoute, existTables);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//非默认数据源
|
||||
if (_entityMetadataManager.IsShardingDataSource(entityType))
|
||||
var entityMetadata = _entityMetadataManager.TryGet(entityType);
|
||||
if (entityMetadata.IsShardingDataSource())
|
||||
{
|
||||
var virtualDataSourceRoute = _dataSourceRouteManager.GetRoute(entityType);
|
||||
//如果是分库对象就要判断是否含有当前数据源
|
||||
if (virtualDataSourceRoute.GetAllDataSourceNames().Contains(dataSourceName))
|
||||
{
|
||||
if (_entityMetadataManager.IsShardingTable(entityType))
|
||||
CreateDataTable(dataSourceName, entityMetadata, existTables);
|
||||
}
|
||||
}
|
||||
//不是分库对象
|
||||
else
|
||||
{
|
||||
var virtualTableRoute = _tableRouteManager.GetRoute(entityType);
|
||||
CreateDataTable(dataSourceName, virtualTableRoute,existTables);
|
||||
}
|
||||
}
|
||||
if (_virtualDataSource.IsDefault(dataSourceName))
|
||||
{
|
||||
CreateDataTable(dataSourceName, entityMetadata, existTables);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -111,6 +104,7 @@ namespace ShardingCore.DynamicDataSources
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void EnsureCreated(bool isDefault, DbContext context,
|
||||
string dataSourceName)
|
||||
{
|
||||
|
@ -138,9 +132,31 @@ namespace ShardingCore.DynamicDataSources
|
|||
$"{nameof(IDbContextCreator)}.{nameof(IDbContextCreator.GetShellDbContext)} db context type not impl {nameof(IShardingDbContext)}");
|
||||
}
|
||||
}
|
||||
private void CreateDataTable(string dataSourceName, IVirtualTableRoute tableRoute, ISet<string> existTables)
|
||||
|
||||
// private void CreateDataTable(string dataSourceName, IVirtualTableRoute tableRoute, ISet<string> existTables)
|
||||
private void CreateDataTable(string dataSourceName, EntityMetadata entityMetadata, ISet<string> existTables)
|
||||
{
|
||||
var entityMetadata = tableRoute.EntityMetadata;
|
||||
if (!entityMetadata.IsShardingTable())
|
||||
{
|
||||
var physicTableName = $"{entityMetadata.LogicTableName}";
|
||||
try
|
||||
{
|
||||
//添加物理表
|
||||
if (!existTables.Contains(physicTableName))
|
||||
_tableCreator.CreateTable(dataSourceName, entityMetadata.EntityType, string.Empty);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
if (!_shardingConfigOptions.IgnoreCreateTableError.GetValueOrDefault())
|
||||
{
|
||||
_logger.LogWarning(e,
|
||||
$"table :{physicTableName} will created.");
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var tableRoute = _tableRouteManager.GetRoute(entityMetadata.EntityType);
|
||||
foreach (var tail in tableRoute.GetTails())
|
||||
{
|
||||
var physicTableName = $"{entityMetadata.LogicTableName}{entityMetadata.TableSeparator}{tail}";
|
||||
|
@ -160,6 +176,6 @@ namespace ShardingCore.DynamicDataSources
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
|
@ -47,6 +47,14 @@ namespace ShardingCore.Extensions
|
|||
var entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager();
|
||||
|
||||
#if NET6_0
|
||||
var entityTypes = contextModel.GetEntityTypes();
|
||||
foreach (var entityType in entityTypes)
|
||||
{
|
||||
if (entityType.GetFieldValue("_data") is List<object> _data)
|
||||
{
|
||||
_data.Clear();
|
||||
}
|
||||
}
|
||||
var contextModelRelationalModel = contextModel.GetRelationalModel() as RelationalModel;
|
||||
var valueTuples =
|
||||
contextModelRelationalModel.Tables.Where(o =>o.Value.EntityTypeMappings.Any(m => entityMetadataManager.IsShardingTable(m.EntityType.ClrType))).Select(o => o.Key).ToList();
|
||||
|
@ -56,6 +64,14 @@ namespace ShardingCore.Extensions
|
|||
}
|
||||
#endif
|
||||
#if NET5_0 || NETSTANDARD2_1
|
||||
var entityTypes = contextModel.GetEntityTypes();
|
||||
foreach (var entityType in entityTypes)
|
||||
{
|
||||
if (entityType.GetFieldValue("_data") is List<object> _data)
|
||||
{
|
||||
_data.Clear();
|
||||
}
|
||||
}
|
||||
var contextModelRelationalModel = contextModel.RelationalModel as RelationalModel;
|
||||
var valueTuples =
|
||||
contextModelRelationalModel.Tables.Where(o => o.Value.EntityTypeMappings.Any(m => entityMetadataManager.IsShardingTable(m.EntityType.ClrType))).Select(o => o.Key).ToList();
|
||||
|
@ -67,6 +83,13 @@ namespace ShardingCore.Extensions
|
|||
#if NETCOREAPP2_0 || NETSTANDARD2_0 || NETCOREAPP3_0
|
||||
var entityTypes =
|
||||
contextModel.GetFieldValue("_entityTypes") as SortedDictionary<string, EntityType>;
|
||||
foreach (var entityType in entityTypes)
|
||||
{
|
||||
if (entityType.GetFieldValue("_data") is List<object> _data)
|
||||
{
|
||||
_data.Clear();
|
||||
}
|
||||
}
|
||||
var list = entityTypes.Where(o=>entityMetadataManager.IsShardingTable(o.Value.ClrType)).Select(o=>o.Key).ToList();
|
||||
for (int i = 0; i < list.Count; i++)
|
||||
{
|
||||
|
@ -124,6 +147,14 @@ namespace ShardingCore.Extensions
|
|||
var entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager();
|
||||
|
||||
#if NET6_0
|
||||
var entityTypes = contextModel.GetEntityTypes();
|
||||
foreach (var entityType in entityTypes)
|
||||
{
|
||||
if (entityType.GetFieldValue("_data") is List<object> _data)
|
||||
{
|
||||
_data.Clear();
|
||||
}
|
||||
}
|
||||
var contextModelRelationalModel = contextModel.GetRelationalModel() as RelationalModel;
|
||||
var valueTuples =
|
||||
contextModelRelationalModel.Tables.Where(o => o.Value.EntityTypeMappings.Any(m => !entityMetadataManager.IsShardingDataSource(m.EntityType.ClrType) ||entityMetadataManager.TryGet(m.EntityType.ClrType)==null)).Select(o => o.Key).ToList();
|
||||
|
@ -133,6 +164,14 @@ namespace ShardingCore.Extensions
|
|||
}
|
||||
#endif
|
||||
#if NET5_0 || NETSTANDARD2_1
|
||||
var entityTypes = contextModel.GetEntityTypes();
|
||||
foreach (var entityType in entityTypes)
|
||||
{
|
||||
if (entityType.GetFieldValue("_data") is List<object> _data)
|
||||
{
|
||||
_data.Clear();
|
||||
}
|
||||
}
|
||||
var contextModelRelationalModel = contextModel.RelationalModel as RelationalModel;
|
||||
var valueTuples =
|
||||
contextModelRelationalModel.Tables.Where(o => o.Value.EntityTypeMappings.Any(m => !entityMetadataManager.IsShardingDataSource(m.EntityType.ClrType)||entityMetadataManager.TryGet(m.EntityType.ClrType)==null)).Select(o => o.Key).ToList();
|
||||
|
@ -142,8 +181,16 @@ namespace ShardingCore.Extensions
|
|||
}
|
||||
#endif
|
||||
#if NETCOREAPP2_0 || NETSTANDARD2_0 || NETCOREAPP3_0
|
||||
|
||||
var entityTypes =
|
||||
contextModel.GetFieldValue("_entityTypes") as SortedDictionary<string, EntityType>;
|
||||
foreach (var entityType in entityTypes)
|
||||
{
|
||||
if (entityType.GetFieldValue("_data") is List<object> _data)
|
||||
{
|
||||
_data.Clear();
|
||||
}
|
||||
}
|
||||
var list = entityTypes.Where(o => !entityMetadataManager.IsShardingDataSource(o.Value.ClrType) || entityMetadataManager.TryGet(o.Value.ClrType) == null).Select(o => o.Key).ToList();
|
||||
for (int i = 0; i < list.Count; i++)
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue