修复tablecreator在有hasdata的时候无法创建表的bug
This commit is contained in:
parent
4fab616b70
commit
5d2a3b160f
|
@ -33,6 +33,9 @@ namespace Sample.MySql.DbContexts
|
|||
_configureGlobalFiltersMethodInfo?.MakeGenericMethod(entityType.ClrType)
|
||||
.Invoke(this, new object[] { modelBuilder, entityType });
|
||||
}
|
||||
|
||||
modelBuilder.Entity<SysUserLogByMonth>().HasData(new SysUserLogByMonth() { Id = "1", Time = DateTime.Now });
|
||||
modelBuilder.Entity<SysTest>().HasData(new SysTest() { Id = "1", UserId = "123" });
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -196,21 +196,21 @@ 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())
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
defaultShardingDbContext.Database.Migrate();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
// using (var scope = app.ApplicationServices.CreateScope())
|
||||
// {
|
||||
// var defaultShardingDbContext = scope.ServiceProvider.GetService<DefaultShardingDbContext>();
|
||||
// // if (defaultShardingDbContext.Database.GetPendingMigrations().Any())
|
||||
// {
|
||||
// try
|
||||
// {
|
||||
//
|
||||
// defaultShardingDbContext.Database.Migrate();
|
||||
// }
|
||||
// catch (Exception e)
|
||||
// {
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// using (var scope = app.ApplicationServices.CreateScope())
|
||||
// {
|
||||
// var defaultShardingDbContext = scope.ServiceProvider.GetService<OtherDbContext>();
|
||||
|
|
|
@ -192,15 +192,23 @@ namespace ShardingCore.Extensions
|
|||
public static void RemoveDbContextRelationModelSaveOnlyThatIsNamedType(this DbContext dbContext,
|
||||
Type shardingType)
|
||||
{
|
||||
#if !EFCORE2&&!EFCORE3&&!EFCORE5&&!EFCORE6
|
||||
throw new NotImplementedException();
|
||||
#if !EFCORE2 && !EFCORE3 && !EFCORE5 && !EFCORE6
|
||||
1
|
||||
#endif
|
||||
#if EFCORE2 ||EFCORE3 ||EFCORE5
|
||||
|
||||
var contextModel = dbContext.Model as Model;
|
||||
#endif
|
||||
#if EFCORE6
|
||||
var contextModel = dbContext.GetService<IDesignTimeModel>().Model; ;
|
||||
var contextModel = dbContext.GetService<IDesignTimeModel>().Model;
|
||||
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
|
||||
|
@ -214,6 +222,15 @@ namespace ShardingCore.Extensions
|
|||
|
||||
#if EFCORE5
|
||||
var contextModelRelationalModel = contextModel.RelationalModel as RelationalModel;
|
||||
|
||||
var entityTypes = contextModel.GetEntityTypes();
|
||||
foreach (var entityType in entityTypes)
|
||||
{
|
||||
if (entityType.GetFieldValue("_data") is List<object> _data)
|
||||
{
|
||||
_data.Clear();
|
||||
}
|
||||
}
|
||||
var valueTuples = contextModelRelationalModel.Tables
|
||||
.Where(o => o.Value.EntityTypeMappings.All(m => m.EntityType.ClrType != shardingType))
|
||||
.Select(o => o.Key).ToList();
|
||||
|
@ -225,6 +242,14 @@ namespace ShardingCore.Extensions
|
|||
#if EFCORE2 || EFCORE3
|
||||
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=>o.Value.ClrType!=shardingType).Select(o=>o.Key).ToList();
|
||||
for (int i = 0; i < list.Count; i++)
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue