发布x.3.1.74

This commit is contained in:
xuejiaming 2021-12-10 11:20:31 +08:00
parent 7703c80a1a
commit 91e2c3b678
4 changed files with 34 additions and 9 deletions

View File

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

View File

@ -253,7 +253,7 @@ namespace ShardingCore.Bootstrapers
}
else
{
dbContext.RemoveDbContextAllRelationModel();
dbContext.RemoveDbContextAllRelationModelThatIsNoShardingTable();
}
dbContext.Database.EnsureCreated();
dbContext.RemoveModelCache();

View File

@ -41,6 +41,16 @@ namespace ShardingCore
*/
public static class DIExtension
{
/// <summary>
/// 添加ShardingCore配置和EntityFrameworkCore的<![CDATA[services.AddDbContext<TShardingDbContext>]]>
/// </summary>
/// <typeparam name="TShardingDbContext"></typeparam>
/// <param name="services"></param>
/// <param name="optionsAction"></param>
/// <param name="contextLifetime"></param>
/// <param name="optionsLifetime"></param>
/// <returns></returns>
/// <exception cref="NotSupportedException"></exception>
public static ShardingCoreConfigBuilder<TShardingDbContext> AddShardingDbContext<TShardingDbContext>(this IServiceCollection services,
Action<string, DbContextOptionsBuilder> optionsAction = null,
ServiceLifetime contextLifetime = ServiceLifetime.Scoped,

View File

@ -70,7 +70,7 @@ namespace ShardingCore.Extensions
/// 移除所有的分表关系的模型
/// </summary>
/// <param name="dbContext"></param>
public static void RemoveDbContextAllRelationModel(this DbContext dbContext)
public static void RemoveDbContextAllRelationModelThatIsNoShardingTable(this DbContext dbContext)
{
#if EFCORE6
@ -80,19 +80,34 @@ namespace ShardingCore.Extensions
var contextModel = dbContext.Model as Model;
#endif
var entityMetadataManager = (IEntityMetadataManager)ShardingContainer.GetService(typeof(IEntityMetadataManager<>).GetGenericType0(dbContext.GetType()));
#if EFCORE6
var contextModelRelationalModel = contextModel.GetRelationalModel() as RelationalModel;
contextModelRelationalModel.Tables.Clear();
var valueTuples =
contextModelRelationalModel.Tables.Where(o => o.Value.EntityTypeMappings.Any(m => entityMetadataManager.IsShardingTable(m.EntityType.ClrType) ||entityMetadataManager.TryGet(m.EntityType.ClrType)==null)).Select(o => o.Key).ToList();
for (int i = 0; i < valueTuples.Count; i++)
{
contextModelRelationalModel.Tables.Remove(valueTuples[i]);
}
#endif
#if EFCORE5
var contextModelRelationalModel = contextModel.RelationalModel as RelationalModel;
contextModelRelationalModel.Tables.Clear();
var valueTuples =
contextModelRelationalModel.Tables.Where(o => o.Value.EntityTypeMappings.Any(m => entityMetadataManager.IsShardingTable(m.EntityType.ClrType)||entityMetadataManager.TryGet(m.EntityType.ClrType)==null)).Select(o => o.Key).ToList();
for (int i = 0; i < valueTuples.Count; i++)
{
contextModelRelationalModel.Tables.Remove(valueTuples[i]);
}
#endif
#if EFCORE2 || EFCORE3
var entityTypes =
contextModel.GetFieldValue("_entityTypes") as SortedDictionary<string, EntityType>;
entityTypes.Clear();
var list = entityTypes.Where(o=>entityMetadataManager.IsShardingTable(o.Value.ClrType)||entityMetadataManager.TryGet(o.Value.ClrType)==null).Select(o=>o.Key).ToList();
for (int i = 0; i < list.Count; i++)
{
entityTypes.Remove(list[i]);
}
#endif
}