添加存在外键时的创建表的bug
This commit is contained in:
parent
d0047ea21a
commit
f672eb9446
|
@ -18,6 +18,7 @@ namespace Samples.AutoByDate.SqlServer.DbContexts
|
||||||
base.OnModelCreating(modelBuilder);
|
base.OnModelCreating(modelBuilder);
|
||||||
modelBuilder.ApplyConfiguration(new SysUserLogByDayMap());
|
modelBuilder.ApplyConfiguration(new SysUserLogByDayMap());
|
||||||
modelBuilder.ApplyConfiguration(new TestLogByWeekMap());
|
modelBuilder.ApplyConfiguration(new TestLogByWeekMap());
|
||||||
|
modelBuilder.ApplyConfiguration(new SysUserLog1ByDayMap());
|
||||||
}
|
}
|
||||||
|
|
||||||
public IRouteTail RouteTail { get; set; }
|
public IRouteTail RouteTail { get; set; }
|
||||||
|
|
|
@ -0,0 +1,15 @@
|
||||||
|
using System;
|
||||||
|
using System.ComponentModel.DataAnnotations.Schema;
|
||||||
|
|
||||||
|
namespace Samples.AutoByDate.SqlServer.Domain.Entities
|
||||||
|
{
|
||||||
|
public class SysUserLog1ByDay
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
public string Body { get; set; }
|
||||||
|
public DateTime CreateTime { get; set; }
|
||||||
|
public int UserId { get; set; }
|
||||||
|
[ForeignKey("UserId")]
|
||||||
|
public virtual SysUserLogByDay SysUserLog { get; set; }
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,19 @@
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||||
|
using Samples.AutoByDate.SqlServer.Domain.Entities;
|
||||||
|
|
||||||
|
namespace Samples.AutoByDate.SqlServer.Domain.Maps
|
||||||
|
{
|
||||||
|
public class SysUserLog1ByDayMap:IEntityTypeConfiguration<SysUserLog1ByDay>
|
||||||
|
{
|
||||||
|
public void Configure(EntityTypeBuilder<SysUserLog1ByDay> builder)
|
||||||
|
{
|
||||||
|
builder.HasKey(o => o.Id);
|
||||||
|
builder.Property(o => o.Id).ValueGeneratedOnAdd();
|
||||||
|
builder.Property(o => o.Body).HasMaxLength(128);
|
||||||
|
//builder.HasOne(o => o.SysUserLog).WithOne(o => o.SysUserLog1)
|
||||||
|
// .HasForeignKey<SysUserLog1ByDay>(o => o.UserId);
|
||||||
|
builder.ToTable(nameof(SysUserLog1ByDay));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,38 @@
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
using System.Linq;
|
||||||
|
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
|
||||||
|
using Microsoft.EntityFrameworkCore.Metadata;
|
||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
using Microsoft.EntityFrameworkCore.Migrations.Internal;
|
||||||
|
using Microsoft.EntityFrameworkCore.Migrations.Operations;
|
||||||
|
using Microsoft.EntityFrameworkCore.Storage;
|
||||||
|
using Microsoft.EntityFrameworkCore.Update;
|
||||||
|
using Microsoft.EntityFrameworkCore.Update.Internal;
|
||||||
|
using Samples.AutoByDate.SqlServer.Domain.Entities;
|
||||||
|
|
||||||
|
namespace Samples.AutoByDate.SqlServer
|
||||||
|
{
|
||||||
|
[SuppressMessage("Usage", "EF1001:Internal EF Core API usage.", Justification = "<挂起>")]
|
||||||
|
public class RemoveForeignKeyMigrationsModelDiffer : MigrationsModelDiffer
|
||||||
|
{
|
||||||
|
private readonly ISet<string> _ignoreForeignKeys=new HashSet<string>()
|
||||||
|
{
|
||||||
|
nameof(SysUserLog1ByDay)
|
||||||
|
};
|
||||||
|
public RemoveForeignKeyMigrationsModelDiffer(IRelationalTypeMappingSource typeMappingSource, IMigrationsAnnotationProvider migrationsAnnotations, IChangeDetector changeDetector, IUpdateAdapterFactory updateAdapterFactory, CommandBatchPreparerDependencies commandBatchPreparerDependencies) : base(typeMappingSource, migrationsAnnotations, changeDetector, updateAdapterFactory, commandBatchPreparerDependencies)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public override IReadOnlyList<MigrationOperation> GetDifferences(IRelationalModel? source, IRelationalModel? target)
|
||||||
|
{
|
||||||
|
var sourceOperations = base.GetDifferences(source, target).ToList();
|
||||||
|
sourceOperations.RemoveAll(x => (x is AddForeignKeyOperation addForeignKey && _ignoreForeignKeys.Contains(addForeignKey.Table)) || (x is DropForeignKeyOperation dropForeignKey && _ignoreForeignKeys.Contains(dropForeignKey.Table)));
|
||||||
|
foreach (var operation in sourceOperations.OfType<CreateTableOperation>())
|
||||||
|
{
|
||||||
|
operation.ForeignKeys?.Clear();
|
||||||
|
}
|
||||||
|
return sourceOperations;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,80 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
using System.Linq;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
|
||||||
|
using Microsoft.EntityFrameworkCore.Metadata;
|
||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
using Microsoft.EntityFrameworkCore.Migrations.Internal;
|
||||||
|
using Microsoft.EntityFrameworkCore.Migrations.Operations;
|
||||||
|
using Microsoft.EntityFrameworkCore.Storage;
|
||||||
|
using Microsoft.EntityFrameworkCore.Update;
|
||||||
|
using Microsoft.EntityFrameworkCore.Update.Internal;
|
||||||
|
using Samples.AutoByDate.SqlServer.Domain.Entities;
|
||||||
|
using ShardingCore.Helpers;
|
||||||
|
using ShardingCore.Sharding.Abstractions;
|
||||||
|
|
||||||
|
namespace Samples.AutoByDate.SqlServer
|
||||||
|
{
|
||||||
|
public class ShardingIgnoreSqlServerMigrationsSqlGenerator<TShardingDbContext> : SqlServerMigrationsSqlGenerator
|
||||||
|
where TShardingDbContext : DbContext, IShardingDbContext
|
||||||
|
{
|
||||||
|
private ISet<string> _ignoreForeignKeyTables = new HashSet<string>()
|
||||||
|
{
|
||||||
|
nameof(SysUserLog1ByDay)
|
||||||
|
};
|
||||||
|
public ShardingIgnoreSqlServerMigrationsSqlGenerator(MigrationsSqlGeneratorDependencies dependencies, IRelationalAnnotationProvider migrationsAnnotations) : base(dependencies, migrationsAnnotations)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void Generate(CreateTableOperation operation, IModel? model, MigrationCommandListBuilder builder,
|
||||||
|
bool terminate = true)
|
||||||
|
{
|
||||||
|
Console.WriteLine("123123");
|
||||||
|
base.Generate(operation, model, builder, terminate);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void Generate(DropForeignKeyOperation operation, IModel? model, MigrationCommandListBuilder builder,
|
||||||
|
bool terminate = true)
|
||||||
|
{
|
||||||
|
if (_ignoreForeignKeyTables.Contains(operation.Table))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
base.Generate(operation, model, builder, terminate);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void Generate(AddForeignKeyOperation operation, IModel? model, MigrationCommandListBuilder builder,
|
||||||
|
bool terminate = true)
|
||||||
|
{
|
||||||
|
if (_ignoreForeignKeyTables.Contains(operation.Table))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
base.Generate(operation, model, builder, terminate);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// https://github.com/Coldairarrow/EFCore.Sharding/blob/master/src/EFCore.Sharding.SqlServer/ShardingSqlServerMigrationsSqlGenerator.cs
|
||||||
|
/// </summary>
|
||||||
|
public class ShardingSqlServerMigrationsSqlGenerator<TShardingDbContext> : SqlServerMigrationsSqlGenerator where TShardingDbContext : DbContext, IShardingDbContext
|
||||||
|
{
|
||||||
|
public ShardingSqlServerMigrationsSqlGenerator(MigrationsSqlGeneratorDependencies dependencies, IRelationalAnnotationProvider migrationsAnnotations) : base(dependencies, migrationsAnnotations)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void Generate(
|
||||||
|
MigrationOperation operation,
|
||||||
|
IModel model,
|
||||||
|
MigrationCommandListBuilder builder)
|
||||||
|
{
|
||||||
|
var oldCmds = builder.GetCommandList().ToList();
|
||||||
|
base.Generate(operation, model, builder);
|
||||||
|
var newCmds = builder.GetCommandList().ToList();
|
||||||
|
var addCmds = newCmds.Where(x => !oldCmds.Contains(x)).ToList();
|
||||||
|
|
||||||
|
MigrationHelper.Generate<TShardingDbContext>(operation, builder, Dependencies.SqlGenerationHelper, addCmds);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,34 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using Samples.AutoByDate.SqlServer.Domain.Entities;
|
||||||
|
using ShardingCore.Core.EntityMetadatas;
|
||||||
|
using ShardingCore.VirtualRoutes.Days;
|
||||||
|
|
||||||
|
namespace Samples.AutoByDate.SqlServer.Shardings
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
* @Author: xjm
|
||||||
|
* @Description:
|
||||||
|
* @Date: Tuesday, 02 February 2021 17:14:53
|
||||||
|
* @Email: 326308290@qq.com
|
||||||
|
*/
|
||||||
|
public class SysUserLog1ByDayVirtualTableRoute:AbstractSimpleShardingDayKeyDateTimeVirtualTableRoute<SysUserLog1ByDay>
|
||||||
|
{
|
||||||
|
public override DateTime GetBeginTime()
|
||||||
|
{
|
||||||
|
//必须返回固定值比如new DateTime(2021,1,1)
|
||||||
|
//如果返回动态值会导致程序重新启动这个值就会变动导致无法获取之前的表
|
||||||
|
return DateTime.Now.AddDays(-2);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override bool AutoCreateTableByTime()
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Configure(EntityMetadataTableBuilder<SysUserLog1ByDay> builder)
|
||||||
|
{
|
||||||
|
builder.ShardingProperty(o => o.CreateTime);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -7,6 +7,7 @@ using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.AspNetCore.HttpsPolicy;
|
using Microsoft.AspNetCore.HttpsPolicy;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
|
@ -36,7 +37,9 @@ namespace Samples.AutoByDate.SqlServer
|
||||||
o.CreateShardingTableOnStart = true;
|
o.CreateShardingTableOnStart = true;
|
||||||
o.EnsureCreatedWithOutShardingTable = true;
|
o.EnsureCreatedWithOutShardingTable = true;
|
||||||
o.AddShardingTableRoute<SysUserLogByDayVirtualTableRoute>();
|
o.AddShardingTableRoute<SysUserLogByDayVirtualTableRoute>();
|
||||||
|
o.AddShardingTableRoute<SysUserLog1ByDayVirtualTableRoute>();
|
||||||
o.AddShardingTableRoute<TestLogWeekVirtualRoute>();
|
o.AddShardingTableRoute<TestLogWeekVirtualRoute>();
|
||||||
|
o.UseInnerDbContextConfigure();
|
||||||
})
|
})
|
||||||
.AddConfig(sp =>
|
.AddConfig(sp =>
|
||||||
{
|
{
|
||||||
|
@ -47,7 +50,7 @@ namespace Samples.AutoByDate.SqlServer
|
||||||
});
|
});
|
||||||
sp.UseShardingTransaction((connection, builder) =>
|
sp.UseShardingTransaction((connection, builder) =>
|
||||||
{
|
{
|
||||||
builder.UseSqlServer(connection);
|
builder.UseSqlServer(connection).ReplaceService<IMigrationsModelDiffer, RemoveForeignKeyMigrationsModelDiffer>();
|
||||||
});
|
});
|
||||||
sp.AddDefaultDataSource("ds0", "Data Source=localhost;Initial Catalog=ShardingCoreDBz;Integrated Security=True;");
|
sp.AddDefaultDataSource("ds0", "Data Source=localhost;Initial Catalog=ShardingCoreDBz;Integrated Security=True;");
|
||||||
}).EnsureConfig();
|
}).EnsureConfig();
|
||||||
|
|
|
@ -49,7 +49,7 @@ namespace ShardingCore.Core.ShardingConfigurations
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 忽略建表时的错误
|
/// 忽略建表时的错误
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool? IgnoreCreateTableError { get; set; } = true;
|
public bool? IgnoreCreateTableError { get; set; } = false;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 添加分表路由
|
/// 添加分表路由
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
@ -28,7 +28,7 @@ namespace ShardingCore.Sharding.Visitors.ShardingExtractParameters
|
||||||
{
|
{
|
||||||
var genericMethodDefinition = node.Method.GetGenericMethodDefinition();
|
var genericMethodDefinition = node.Method.GetGenericMethodDefinition();
|
||||||
|
|
||||||
// find cachable query extention calls
|
// find notsupport extention calls
|
||||||
if (genericMethodDefinition == EntityFrameworkShardingQueryableExtension.NotSupportMethodInfo)
|
if (genericMethodDefinition == EntityFrameworkShardingQueryableExtension.NotSupportMethodInfo)
|
||||||
{
|
{
|
||||||
isNotSupport = true;
|
isNotSupport = true;
|
||||||
|
|
Loading…
Reference in New Issue