diff --git a/samples/Samples.AutoByDate.SqlServer/DbContexts/DefaultShardingDbContext.cs b/samples/Samples.AutoByDate.SqlServer/DbContexts/DefaultShardingDbContext.cs index 8e734911..7dd46d3b 100644 --- a/samples/Samples.AutoByDate.SqlServer/DbContexts/DefaultShardingDbContext.cs +++ b/samples/Samples.AutoByDate.SqlServer/DbContexts/DefaultShardingDbContext.cs @@ -18,6 +18,7 @@ namespace Samples.AutoByDate.SqlServer.DbContexts base.OnModelCreating(modelBuilder); modelBuilder.ApplyConfiguration(new SysUserLogByDayMap()); modelBuilder.ApplyConfiguration(new TestLogByWeekMap()); + modelBuilder.ApplyConfiguration(new SysUserLog1ByDayMap()); } public IRouteTail RouteTail { get; set; } diff --git a/samples/Samples.AutoByDate.SqlServer/Domain/Entities/SysUserLog1ByDay.cs b/samples/Samples.AutoByDate.SqlServer/Domain/Entities/SysUserLog1ByDay.cs new file mode 100644 index 00000000..18a68bc9 --- /dev/null +++ b/samples/Samples.AutoByDate.SqlServer/Domain/Entities/SysUserLog1ByDay.cs @@ -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; } + } +} diff --git a/samples/Samples.AutoByDate.SqlServer/Domain/Maps/SysUserLog1ByDayMap.cs b/samples/Samples.AutoByDate.SqlServer/Domain/Maps/SysUserLog1ByDayMap.cs new file mode 100644 index 00000000..155ead4c --- /dev/null +++ b/samples/Samples.AutoByDate.SqlServer/Domain/Maps/SysUserLog1ByDayMap.cs @@ -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 + { + public void Configure(EntityTypeBuilder 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(o => o.UserId); + builder.ToTable(nameof(SysUserLog1ByDay)); + } + } +} diff --git a/samples/Samples.AutoByDate.SqlServer/RemoveForeignKeyMigrationsModelDiffer.cs b/samples/Samples.AutoByDate.SqlServer/RemoveForeignKeyMigrationsModelDiffer.cs new file mode 100644 index 00000000..bdbac891 --- /dev/null +++ b/samples/Samples.AutoByDate.SqlServer/RemoveForeignKeyMigrationsModelDiffer.cs @@ -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 _ignoreForeignKeys=new HashSet() + { + nameof(SysUserLog1ByDay) + }; + public RemoveForeignKeyMigrationsModelDiffer(IRelationalTypeMappingSource typeMappingSource, IMigrationsAnnotationProvider migrationsAnnotations, IChangeDetector changeDetector, IUpdateAdapterFactory updateAdapterFactory, CommandBatchPreparerDependencies commandBatchPreparerDependencies) : base(typeMappingSource, migrationsAnnotations, changeDetector, updateAdapterFactory, commandBatchPreparerDependencies) + { + } + + public override IReadOnlyList 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()) + { + operation.ForeignKeys?.Clear(); + } + return sourceOperations; + } + } +} diff --git a/samples/Samples.AutoByDate.SqlServer/ShardingSqlServerMigrationsSqlGenerator.cs b/samples/Samples.AutoByDate.SqlServer/ShardingSqlServerMigrationsSqlGenerator.cs new file mode 100644 index 00000000..93703624 --- /dev/null +++ b/samples/Samples.AutoByDate.SqlServer/ShardingSqlServerMigrationsSqlGenerator.cs @@ -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 : SqlServerMigrationsSqlGenerator + where TShardingDbContext : DbContext, IShardingDbContext + { + private ISet _ignoreForeignKeyTables = new HashSet() + { + 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); + } + } + /// + /// https://github.com/Coldairarrow/EFCore.Sharding/blob/master/src/EFCore.Sharding.SqlServer/ShardingSqlServerMigrationsSqlGenerator.cs + /// + public class ShardingSqlServerMigrationsSqlGenerator : 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(operation, builder, Dependencies.SqlGenerationHelper, addCmds); + } + } +} diff --git a/samples/Samples.AutoByDate.SqlServer/Shardings/SysUserLog1ByDayVirtualTableRoute.cs b/samples/Samples.AutoByDate.SqlServer/Shardings/SysUserLog1ByDayVirtualTableRoute.cs new file mode 100644 index 00000000..91991d18 --- /dev/null +++ b/samples/Samples.AutoByDate.SqlServer/Shardings/SysUserLog1ByDayVirtualTableRoute.cs @@ -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 + { + 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 builder) + { + builder.ShardingProperty(o => o.CreateTime); + } + } +} \ No newline at end of file diff --git a/samples/Samples.AutoByDate.SqlServer/Startup.cs b/samples/Samples.AutoByDate.SqlServer/Startup.cs index 675dc61c..a0b46f3c 100644 --- a/samples/Samples.AutoByDate.SqlServer/Startup.cs +++ b/samples/Samples.AutoByDate.SqlServer/Startup.cs @@ -7,6 +7,7 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.HttpsPolicy; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Migrations; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; @@ -36,7 +37,9 @@ namespace Samples.AutoByDate.SqlServer o.CreateShardingTableOnStart = true; o.EnsureCreatedWithOutShardingTable = true; o.AddShardingTableRoute(); + o.AddShardingTableRoute(); o.AddShardingTableRoute(); + o.UseInnerDbContextConfigure(); }) .AddConfig(sp => { @@ -47,7 +50,7 @@ namespace Samples.AutoByDate.SqlServer }); sp.UseShardingTransaction((connection, builder) => { - builder.UseSqlServer(connection); + builder.UseSqlServer(connection).ReplaceService(); }); sp.AddDefaultDataSource("ds0", "Data Source=localhost;Initial Catalog=ShardingCoreDBz;Integrated Security=True;"); }).EnsureConfig(); diff --git a/src/ShardingCore/Core/ShardingConfigurations/ShardingEntityConfigOptions.cs b/src/ShardingCore/Core/ShardingConfigurations/ShardingEntityConfigOptions.cs index 57a10cad..9f72e410 100644 --- a/src/ShardingCore/Core/ShardingConfigurations/ShardingEntityConfigOptions.cs +++ b/src/ShardingCore/Core/ShardingConfigurations/ShardingEntityConfigOptions.cs @@ -49,7 +49,7 @@ namespace ShardingCore.Core.ShardingConfigurations /// /// 忽略建表时的错误 /// - public bool? IgnoreCreateTableError { get; set; } = true; + public bool? IgnoreCreateTableError { get; set; } = false; /// /// 添加分表路由 /// diff --git a/src/ShardingCore/Sharding/Visitors/ShardingExtractParameters/ShardingQueryableExtractParameterVisitor.cs b/src/ShardingCore/Sharding/Visitors/ShardingExtractParameters/ShardingQueryableExtractParameterVisitor.cs index a7c0553d..4a53d3e3 100644 --- a/src/ShardingCore/Sharding/Visitors/ShardingExtractParameters/ShardingQueryableExtractParameterVisitor.cs +++ b/src/ShardingCore/Sharding/Visitors/ShardingExtractParameters/ShardingQueryableExtractParameterVisitor.cs @@ -28,7 +28,7 @@ namespace ShardingCore.Sharding.Visitors.ShardingExtractParameters { var genericMethodDefinition = node.Method.GetGenericMethodDefinition(); - // find cachable query extention calls + // find notsupport extention calls if (genericMethodDefinition == EntityFrameworkShardingQueryableExtension.NotSupportMethodInfo) { isNotSupport = true;