发布x.3.1.73修复未分表对象在非默认数据源中创建了表

This commit is contained in:
xuejiaming 2021-12-09 21:04:55 +08:00
parent dce2fe36e2
commit 7703c80a1a
7 changed files with 93 additions and 18 deletions

View File

@ -1,9 +1,9 @@
:start
::定义版本
set EFCORE2=2.3.1.72
set EFCORE3=3.3.1.72
set EFCORE5=5.3.1.72
set EFCORE6=6.3.1.72
set EFCORE2=2.3.1.73
set EFCORE3=3.3.1.73
set EFCORE5=5.3.1.73
set EFCORE6=6.3.1.73
::删除所有bin与obj下的文件
@echo off

View File

@ -0,0 +1,18 @@
namespace Sample.SqlServerShardingAll.Entities
{
public class NoShardingTest
{
/// <summary>
/// user id
/// </summary>
public string Id { get; set; }
/// <summary>
/// user name
/// </summary>
public string Name { get; set; }
/// <summary>
/// area
/// </summary>
public string Area { get; set; }
}
}

View File

@ -32,6 +32,14 @@ namespace Sample.SqlServerShardingAll
entity.Property(o=>o.Area).IsRequired().IsUnicode(false).HasMaxLength(50);
entity.ToTable(nameof(SysUser));
});
modelBuilder.Entity<NoShardingTest>(entity =>
{
entity.HasKey(o => o.Id);
entity.Property(o => o.Id).IsRequired().IsUnicode(false).HasMaxLength(50);
entity.Property(o=>o.Name).IsRequired().IsUnicode(false).HasMaxLength(50);
entity.Property(o=>o.Area).IsRequired().IsUnicode(false).HasMaxLength(50);
entity.ToTable(nameof(NoShardingTest));
});
}
public IRouteTail RouteTail { get; set; }

View File

@ -79,21 +79,12 @@ namespace ShardingCore.Bootstrapers
public void Init()
{
_virtualDataSource.AddPhysicDataSource(new DefaultPhysicDataSource(_shardingConfigOption.DefaultDataSourceName, _shardingConfigOption.DefaultConnectionString, true));
InitializeParallelTables();
InitializeEntityMetadata();
InitializeParallelTables();
InitializeConfigure();
_virtualDataSource.CheckVirtualDataSource();
}
private void InitializeParallelTables()
{
foreach (var parallelTableGroupNode in _shardingConfigOption.GetParallelTableGroupNodes())
{
_parallelTableManager.AddParallelTable(parallelTableGroupNode);
}
}
private void InitializeEntityMetadata()
{
using (var serviceScope = ShardingContainer.ServiceProvider.CreateScope())
@ -133,9 +124,22 @@ namespace ShardingCore.Bootstrapers
}
private void InitializeParallelTables()
{
foreach (var parallelTableGroupNode in _shardingConfigOption.GetParallelTableGroupNodes())
{
var parallelTableComparerType = parallelTableGroupNode.GetEntities().FirstOrDefault(o => !_entityMetadataManager.IsShardingTable(o.Type));
if (parallelTableComparerType != null)
{
throw new ShardingCoreInvalidOperationException(
$"{parallelTableComparerType.Type.Name} must is sharding table type");
}
_parallelTableManager.AddParallelTable(parallelTableGroupNode);
}
}
private void InitializeConfigure()
{
var dataSources = _shardingConfigOption.GetDataSources();
foreach (var dataSourceKv in dataSources)
{
@ -230,7 +234,9 @@ namespace ShardingCore.Bootstrapers
if (context is IShardingDbContext shardingDbContext)
{
var dbContext = shardingDbContext.GetDbContext(dataSourceName, false, _routeTailFactory.Create(string.Empty));
var isDefault = _virtualDataSource.IsDefault(dataSourceName);
var modelCacheSyncObject = dbContext.GetModelCacheSyncObject();
var acquire = Monitor.TryEnter(modelCacheSyncObject, TimeSpan.FromSeconds(3));
@ -241,7 +247,14 @@ namespace ShardingCore.Bootstrapers
try
{
dbContext.RemoveDbContextRelationModelThatIsShardingTable();
if(isDefault)
{
dbContext.RemoveDbContextRelationModelThatIsShardingTable();
}
else
{
dbContext.RemoveDbContextAllRelationModel();
}
dbContext.Database.EnsureCreated();
dbContext.RemoveModelCache();
}

View File

@ -66,6 +66,35 @@ namespace ShardingCore.Extensions
}
#endif
}
/// <summary>
/// 移除所有的分表关系的模型
/// </summary>
/// <param name="dbContext"></param>
public static void RemoveDbContextAllRelationModel(this DbContext dbContext)
{
#if EFCORE6
var contextModel = dbContext.GetService<IDesignTimeModel>().Model; ;
#endif
#if EFCORE2 || EFCORE3 || EFCORE5
var contextModel = dbContext.Model as Model;
#endif
#if EFCORE6
var contextModelRelationalModel = contextModel.GetRelationalModel() as RelationalModel;
contextModelRelationalModel.Tables.Clear();
#endif
#if EFCORE5
var contextModelRelationalModel = contextModel.RelationalModel as RelationalModel;
contextModelRelationalModel.Tables.Clear();
#endif
#if EFCORE2 || EFCORE3
var entityTypes =
contextModel.GetFieldValue("_entityTypes") as SortedDictionary<string, EntityType>;
entityTypes.Clear();
#endif
}
/// <summary>
/// 移除所有的除了我指定的那个类型

View File

@ -18,6 +18,11 @@ namespace ShardingCore.Sharding.ParallelTables
{
_entities = new SortedSet<ParallelTableComparerType>(entities);
}
public ISet<ParallelTableComparerType> GetEntities()
{
return _entities;
}
protected bool Equals(ParallelTableGroupNode other)
{
return _entities.SequenceEqual(other._entities);

View File

@ -89,7 +89,9 @@ namespace ShardingCore.Sharding
_routeTailFactory = routeTailFactory;
DataSourceRouteResult = dataSourceRouteResult;
_parallelTableManager = (IParallelTableManager)ShardingContainer.GetService(typeof(IParallelTableManager<>).GetGenericType0(shardingDbContext.GetType()));
_entityMetadataManager =
(IEntityMetadataManager)ShardingContainer.GetService(
typeof(IEntityMetadataManager<>).GetGenericType0(shardingDbContext.GetType()));
TableRouteResults = GetTableRouteResults(tableRouteResults);
IsCrossDataSource = dataSourceRouteResult.IntersectDataSources.Count > 1;
IsCrossTable = TableRouteResults.Count() > 1;