合并GetTableName在efcore2和非efcore2下的获取不一致

This commit is contained in:
xuejiaming 2021-12-13 09:30:39 +08:00
parent cb836a25e9
commit a0b7b02f0f
3 changed files with 35 additions and 9 deletions

View File

@ -4,6 +4,7 @@ using System.Text;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Extensions.InternalExtensions;
/*
* @Author: xjm
@ -22,13 +23,7 @@ namespace ShardingCore.Bootstrapers
{
EntityType = entityType;
#if !EFCORE2
var virtualTableName = entityType.GetTableName();
#endif
#if EFCORE2
var virtualTableName = entityType.Relational().TableName;
#endif
VirtualTableName = virtualTableName;
VirtualTableName = entityType.GetEntityTypeTableName();
}
public IEntityType EntityType { get; }

View File

@ -17,6 +17,7 @@ using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
using ShardingCore.Core.VirtualTables;
using ShardingCore.Exceptions;
using ShardingCore.Extensions;
using ShardingCore.Extensions.InternalExtensions;
using ShardingCore.Sharding.Abstractions;
using ShardingCore.Sharding.ParallelTables;
using ShardingCore.TableCreator;
@ -61,7 +62,7 @@ namespace ShardingCore.DynamicDataSources
EnsureCreated(context, dataSourceName);
var tableEnsureManager = ShardingContainer.GetService<ITableEnsureManager<TShardingDbContext>>();
//获取数据库存在的所有的表
var existTables = tableEnsureManager?.GetExistTables(context, dataSourceName)??new HashSet<string>();
var existTables = tableEnsureManager?.GetExistTables(context, dataSourceName) ?? new HashSet<string>();
foreach (var entity in context.Model.GetEntityTypes())
{
var entityType = entity.ClrType;
@ -92,7 +93,8 @@ namespace ShardingCore.DynamicDataSources
}
if (_shardingConfigOption.NeedCreateTable(entityType))
{
_tableCreator.CreateTable(dataSourceName, entityType, string.Empty);
if (!existTables.Contains(entity.GetEntityTypeTableName()))
_tableCreator.CreateTable(dataSourceName, entityType, string.Empty);
}
}
}

View File

@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
namespace ShardingCore.Extensions.InternalExtensions
{
internal static class InternalIEntityTypeExtension
{
/// <summary>
/// 获取在db context内的数据库表名称对应叫什么
/// </summary>
/// <param name="entityType"></param>
/// <returns></returns>
public static string GetEntityTypeTableName(this IEntityType entityType)
{
#if !EFCORE2
var tableName = entityType.GetTableName();
#endif
#if EFCORE2
var tableName = entityType.Relational().TableName;
#endif
return tableName;
}
}
}