发布x.3.1.38添加自动添加表给一些懒惰的小伙伴提供建表
This commit is contained in:
parent
fd3f6682fa
commit
80b8e8d9a5
|
@ -1,9 +1,9 @@
|
|||
:start
|
||||
::定义版本
|
||||
set EFCORE2=2.3.1.37
|
||||
set EFCORE3=3.3.1.37
|
||||
set EFCORE5=5.3.1.37
|
||||
set EFCORE6=6.3.1.37
|
||||
set EFCORE2=2.3.1.38
|
||||
set EFCORE3=3.3.1.38
|
||||
set EFCORE5=5.3.1.38
|
||||
set EFCORE6=6.3.1.38
|
||||
|
||||
::删除所有bin与obj下的文件
|
||||
@echo off
|
||||
|
|
|
@ -8,6 +8,8 @@ using Sample.SqlServer.DbContexts;
|
|||
using Sample.SqlServer.Shardings;
|
||||
using ShardingCore;
|
||||
using System;
|
||||
using Sample.SqlServer.Domain.Entities;
|
||||
using ShardingCore.Extensions;
|
||||
using ShardingCore.Sharding.ShardingComparision;
|
||||
|
||||
namespace Sample.SqlServer
|
||||
|
@ -35,6 +37,8 @@ namespace Sample.SqlServer
|
|||
o.AutoTrackEntity = true;
|
||||
o.ParallelQueryMaxThreadCount = 100;
|
||||
o.ParallelQueryTimeOut=TimeSpan.FromSeconds(10);
|
||||
//if SysTest entity not exists in db and db is exists
|
||||
//o.AddEntityTryCreateTable<SysTest>(); // or `o.AddEntitiesTryCreateTable(typeof(SysTest));`
|
||||
})
|
||||
//.AddShardingQuery((conStr, builder) => builder.UseSqlServer(conStr).UseLoggerFactory(efLogger))//无需添加.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking) 并发查询系统会自动添加NoTracking
|
||||
.AddShardingTransaction((connection, builder) =>
|
||||
|
|
|
@ -130,6 +130,13 @@ namespace ShardingCore.Bootstrapers
|
|||
//创建表
|
||||
CreateDataTable(dataSourceName, virtualTable);
|
||||
}
|
||||
else
|
||||
{
|
||||
if(_shardingConfigOption.NeedCreateTable(entityType))
|
||||
{
|
||||
_tableCreator.CreateTable(dataSourceName, entityType, string.Empty);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -101,7 +101,7 @@ namespace ShardingCore.DIExtensions
|
|||
/// <summary>
|
||||
/// 忽略建表时的错误
|
||||
/// </summary>
|
||||
public bool? IgnoreCreateTableError { get; set; }
|
||||
public bool? IgnoreCreateTableError { get; set; } = true;
|
||||
|
||||
private readonly ISet<Type> _createTableEntities = new HashSet<Type>();
|
||||
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ShardingCore.DIExtensions;
|
||||
using ShardingCore.Sharding.Abstractions;
|
||||
|
||||
namespace ShardingCore.Extensions
|
||||
{
|
||||
public static class ShardingCoreConfigBuilderExtension
|
||||
{
|
||||
public static void AddEntityTryCreateTable<TEntity>(this ShardingCoreBeginOptions source) where TEntity:class
|
||||
{
|
||||
source.AddEntitiesTryCreateTable(typeof(TEntity));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -45,6 +45,7 @@ namespace ShardingCore
|
|||
/// <param name="entityType"></param>
|
||||
/// <returns></returns>
|
||||
public bool AddEntityTryCreateTable(Type entityType);
|
||||
public bool AnyEntityTryCreateTable();
|
||||
/// <summary>
|
||||
/// 是否需要启动创建表
|
||||
/// </summary>
|
||||
|
|
|
@ -207,6 +207,11 @@ namespace ShardingCore
|
|||
return _createTableEntities.Add(entityType);
|
||||
}
|
||||
|
||||
public bool AnyEntityTryCreateTable()
|
||||
{
|
||||
return _createTableEntities.Any();
|
||||
}
|
||||
|
||||
public bool NeedCreateTable(Type entityType)
|
||||
{
|
||||
return _createTableEntities.Contains(entityType);
|
||||
|
@ -215,7 +220,7 @@ namespace ShardingCore
|
|||
/// <summary>
|
||||
/// 忽略建表时的错误
|
||||
/// </summary>
|
||||
public bool? IgnoreCreateTableError { get; set; }
|
||||
public bool? IgnoreCreateTableError { get; set; } = true;
|
||||
/// <summary>
|
||||
/// 自动追踪实体
|
||||
/// </summary>
|
||||
|
|
|
@ -26,27 +26,22 @@ namespace ShardingCore.TableCreator
|
|||
public class ShardingTableCreator<TShardingDbContext> : IShardingTableCreator<TShardingDbContext> where TShardingDbContext : DbContext, IShardingDbContext
|
||||
{
|
||||
private readonly ILogger<ShardingTableCreator<TShardingDbContext>> _logger;
|
||||
private readonly IShardingDbContextFactory<TShardingDbContext> _shardingDbContextFactory;
|
||||
private readonly IVirtualTableManager<TShardingDbContext> _virtualTableManager;
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
private readonly IShardingConfigOption _shardingConfigOption;
|
||||
private readonly IRouteTailFactory _routeTailFactory;
|
||||
|
||||
public ShardingTableCreator(ILogger<ShardingTableCreator<TShardingDbContext>> logger, IShardingDbContextFactory<TShardingDbContext> shardingDbContextFactory,
|
||||
IVirtualTableManager<TShardingDbContext> virtualTableManager, IServiceProvider serviceProvider, IEnumerable<IShardingConfigOption> shardingConfigOptions,IRouteTailFactory routeTailFactory)
|
||||
public ShardingTableCreator(ILogger<ShardingTableCreator<TShardingDbContext>> logger, IServiceProvider serviceProvider, IEnumerable<IShardingConfigOption> shardingConfigOptions, IRouteTailFactory routeTailFactory)
|
||||
{
|
||||
_logger = logger;
|
||||
_shardingDbContextFactory = shardingDbContextFactory;
|
||||
_virtualTableManager = virtualTableManager;
|
||||
_serviceProvider = serviceProvider;
|
||||
_shardingConfigOption = shardingConfigOptions.FirstOrDefault(o => o.ShardingDbContextType == typeof(TShardingDbContext))
|
||||
??throw new ArgumentNullException(typeof(TShardingDbContext).FullName);
|
||||
?? throw new ArgumentNullException(typeof(TShardingDbContext).FullName);
|
||||
_routeTailFactory = routeTailFactory;
|
||||
}
|
||||
|
||||
public void CreateTable<T>(string dataSourceName, string tail) where T : class
|
||||
{
|
||||
CreateTable(dataSourceName,typeof(T), tail);
|
||||
CreateTable(dataSourceName, typeof(T), tail);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -56,39 +51,38 @@ namespace ShardingCore.TableCreator
|
|||
/// <param name="shardingEntityType"></param>
|
||||
/// <param name="tail"></param>
|
||||
/// <exception cref="ShardingCreateException"></exception>
|
||||
public void CreateTable(string dataSourceName,Type shardingEntityType, string tail)
|
||||
public void CreateTable(string dataSourceName, Type shardingEntityType, string tail)
|
||||
{
|
||||
using (var serviceScope = _serviceProvider.CreateScope())
|
||||
{
|
||||
var virtualTable = _virtualTableManager.GetVirtualTable( shardingEntityType);
|
||||
var dbContext = serviceScope.ServiceProvider.GetService<TShardingDbContext>();
|
||||
var shardingDbContext = (IShardingDbContext)dbContext;
|
||||
var context = shardingDbContext.GetDbContext(dataSourceName,false, _routeTailFactory.Create(tail));
|
||||
var context = shardingDbContext.GetDbContext(dataSourceName, false, _routeTailFactory.Create(tail));
|
||||
|
||||
var modelCacheSyncObject = context.GetModelCacheSyncObject();
|
||||
|
||||
lock (modelCacheSyncObject)
|
||||
{
|
||||
context.RemoveDbContextRelationModelSaveOnlyThatIsNamedType(shardingEntityType);
|
||||
|
||||
lock (modelCacheSyncObject)
|
||||
{
|
||||
context.RemoveDbContextRelationModelSaveOnlyThatIsNamedType(shardingEntityType);
|
||||
var databaseCreator = context.Database.GetService<IDatabaseCreator>() as RelationalDatabaseCreator;
|
||||
try
|
||||
try
|
||||
{
|
||||
databaseCreator.CreateTables();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (!_shardingConfigOption.IgnoreCreateTableError.GetValueOrDefault())
|
||||
{
|
||||
databaseCreator.CreateTables();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (!_shardingConfigOption.IgnoreCreateTableError.GetValueOrDefault())
|
||||
{
|
||||
_logger.LogWarning(ex,
|
||||
$"create table error maybe table:[{virtualTable.GetVirtualTableName()}{virtualTable.EntityMetadata.TableSeparator}{tail}].");
|
||||
throw new ShardingCreateException($" create table error :{ex.Message}", ex);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
context.RemoveModelCache();
|
||||
_logger.LogWarning(ex,
|
||||
$"create table error entity name:[{shardingEntityType.Name}].");
|
||||
throw new ShardingCreateException($" create table error :{ex.Message}", ex);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
context.RemoveModelCache();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue