From b7cabfe34c1651625ec3b8a5ed1bfee61a3a8f78 Mon Sep 17 00:00:00 2001 From: xuejiaming <326308290@qq.com> Date: Mon, 22 Mar 2021 11:12:00 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Defcore2.x=20=E7=9A=84mysql?= =?UTF-8?q?=E6=97=A0=E6=B3=95=E4=BD=BF=E7=94=A8=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ShardingCore.sln | 72 +++++++++++-------- nuget-publish.bat | 6 +- .../Controllers/WeatherForecastController.cs | 34 +++++++++ samples/Sample.MySql/DIExtension.cs | 52 ++++++++++++++ .../DbContexts/DefaultTableDbContext.cs | 23 ++++++ .../Sample.MySql/Domain/Entities/SysTest.cs | 8 +++ .../Domain/Entities/SysUserMod.cs | 27 +++++++ .../Sample.MySql/Domain/Maps/SysTestMap.cs | 17 +++++ .../Sample.MySql/Domain/Maps/SysUserModMap.cs | 23 ++++++ samples/Sample.MySql/Program.cs | 26 +++++++ .../Properties/launchSettings.json | 30 ++++++++ samples/Sample.MySql/Sample.MySql.csproj | 12 ++++ .../Shardings/SysUserModVirtualTableRoute.cs | 18 +++++ samples/Sample.MySql/Startup.cs | 64 +++++++++++++++++ samples/Sample.MySql/WeatherForecast.cs | 15 ++++ .../Sample.MySql/appsettings.Development.json | 9 +++ samples/Sample.MySql/appsettings.json | 10 +++ src/ShardingCore.MySql/DIExtension.cs | 1 + .../EFCores/ShardingMySqlQuerySqlGenerator.cs | 2 +- .../Enumerators/StreamMergeAsyncEnumerator.cs | 13 ++-- .../Extensions/DbContextExtension.cs | 14 ++-- src/ShardingCore/ShardingBootstrapper.cs | 1 + 22 files changed, 437 insertions(+), 40 deletions(-) create mode 100644 samples/Sample.MySql/Controllers/WeatherForecastController.cs create mode 100644 samples/Sample.MySql/DIExtension.cs create mode 100644 samples/Sample.MySql/DbContexts/DefaultTableDbContext.cs create mode 100644 samples/Sample.MySql/Domain/Entities/SysTest.cs create mode 100644 samples/Sample.MySql/Domain/Entities/SysUserMod.cs create mode 100644 samples/Sample.MySql/Domain/Maps/SysTestMap.cs create mode 100644 samples/Sample.MySql/Domain/Maps/SysUserModMap.cs create mode 100644 samples/Sample.MySql/Program.cs create mode 100644 samples/Sample.MySql/Properties/launchSettings.json create mode 100644 samples/Sample.MySql/Sample.MySql.csproj create mode 100644 samples/Sample.MySql/Shardings/SysUserModVirtualTableRoute.cs create mode 100644 samples/Sample.MySql/Startup.cs create mode 100644 samples/Sample.MySql/WeatherForecast.cs create mode 100644 samples/Sample.MySql/appsettings.Development.json create mode 100644 samples/Sample.MySql/appsettings.json diff --git a/ShardingCore.sln b/ShardingCore.sln index 1c4ef5bb..aca63651 100644 --- a/ShardingCore.sln +++ b/ShardingCore.sln @@ -1,61 +1,51 @@  Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31019.35 +MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{490FAE47-4476-4508-B216-505FC850447F}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ShardingCore", "src\ShardingCore\ShardingCore.csproj", "{3CAF09A6-6ABD-41D9-BA57-9A822B8095F7}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ShardingCore", "src\ShardingCore\ShardingCore.csproj", "{3CAF09A6-6ABD-41D9-BA57-9A822B8095F7}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ShardingCore.SqlServer", "src\ShardingCore.SqlServer\ShardingCore.SqlServer.csproj", "{2B65466D-D4F8-48FE-A516-014404414897}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ShardingCore.SqlServer", "src\ShardingCore.SqlServer\ShardingCore.SqlServer.csproj", "{2B65466D-D4F8-48FE-A516-014404414897}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src3x", "src3x", "{62AAE0FE-4099-4A48-AA3C-F76F14C62655}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ShardingCore.3x", "src3x\ShardingCore.3x\ShardingCore.3x.csproj", "{E0FDBFA9-AE5F-42FB-8A60-4E5D9E7D5414}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ShardingCore.3x", "src3x\ShardingCore.3x\ShardingCore.3x.csproj", "{E0FDBFA9-AE5F-42FB-8A60-4E5D9E7D5414}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ShardingCore.SqlServer.3x", "src3x\ShardingCore.SqlServer.3x\ShardingCore.SqlServer.3x.csproj", "{37E11FFD-14D7-41B4-B04C-2950206C64DB}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ShardingCore.SqlServer.3x", "src3x\ShardingCore.SqlServer.3x\ShardingCore.SqlServer.3x.csproj", "{37E11FFD-14D7-41B4-B04C-2950206C64DB}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src2x", "src2x", "{679E6084-0C45-4807-BFEE-D2FDA44B2188}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ShardingCore.2x", "src2x\ShardingCore.2x\ShardingCore.2x.csproj", "{FB92A4BE-A43E-4755-8132-EC38E9650B80}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ShardingCore.2x", "src2x\ShardingCore.2x\ShardingCore.2x.csproj", "{FB92A4BE-A43E-4755-8132-EC38E9650B80}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ShardingCore.SqlServer.2x", "src2x\ShardingCore.SqlServer.2x\ShardingCore.SqlServer.2x.csproj", "{73B802A7-3DDE-4B02-9E3D-D6B9EE42DE5D}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ShardingCore.SqlServer.2x", "src2x\ShardingCore.SqlServer.2x\ShardingCore.SqlServer.2x.csproj", "{73B802A7-3DDE-4B02-9E3D-D6B9EE42DE5D}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{CC2C88C0-65F2-445D-BE78-973B840FE281}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ShardingCore.Test50", "test\ShardingCore.Test50\ShardingCore.Test50.csproj", "{7EE133B6-5A02-41B7-9D89-41D9EA14184E}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ShardingCore.Test50", "test\ShardingCore.Test50\ShardingCore.Test50.csproj", "{7EE133B6-5A02-41B7-9D89-41D9EA14184E}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{EDF8869A-C1E1-491B-BC9F-4A33F4DE1C73}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample.SqlServer", "samples\Sample.SqlServer\Sample.SqlServer.csproj", "{D48E5EC2-CF83-4B17-8BBA-BDE52ADFAB1F}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample.SqlServer", "samples\Sample.SqlServer\Sample.SqlServer.csproj", "{D48E5EC2-CF83-4B17-8BBA-BDE52ADFAB1F}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ShardingCore.MySql", "src\ShardingCore.MySql\ShardingCore.MySql.csproj", "{10308339-FF5C-43AB-9208-627B6E2FBBDB}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ShardingCore.MySql", "src\ShardingCore.MySql\ShardingCore.MySql.csproj", "{10308339-FF5C-43AB-9208-627B6E2FBBDB}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ShardingCore.MySql.3x", "src3x\ShardingCore.MySql.3x\ShardingCore.MySql.3x.csproj", "{954C4CA2-9CF4-4C2C-8DE6-180DD8202E38}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ShardingCore.MySql.3x", "src3x\ShardingCore.MySql.3x\ShardingCore.MySql.3x.csproj", "{954C4CA2-9CF4-4C2C-8DE6-180DD8202E38}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ShardingCore.MySql.2x", "src2x\ShardingCore.MySql.2x\ShardingCore.MySql.2x.csproj", "{0CF88F0B-6CCB-49B5-B41D-CDC193B51581}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ShardingCore.MySql.2x", "src2x\ShardingCore.MySql.2x\ShardingCore.MySql.2x.csproj", "{0CF88F0B-6CCB-49B5-B41D-CDC193B51581}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ShardingCore.Test50.MySql", "test\ShardingCore.Test50.MySql\ShardingCore.Test50.MySql.csproj", "{C8FAB96F-F13E-4094-883C-2D38D39EE4A3}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ShardingCore.Test50.MySql", "test\ShardingCore.Test50.MySql\ShardingCore.Test50.MySql.csproj", "{C8FAB96F-F13E-4094-883C-2D38D39EE4A3}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Samples.AutoByDate.SqlServer", "samples\Samples.AutoByDate.SqlServer\Samples.AutoByDate.SqlServer.csproj", "{C34FCF48-1A98-4268-BFEE-6C9BFC7FD539}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Samples.AutoByDate.SqlServer", "samples\Samples.AutoByDate.SqlServer\Samples.AutoByDate.SqlServer.csproj", "{C34FCF48-1A98-4268-BFEE-6C9BFC7FD539}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample.MySql", "samples\Sample.MySql\Sample.MySql.csproj", "{90675788-D5C3-415A-9C18-FF159A75B4D5}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU Release|Any CPU = Release|Any CPU EndGlobalSection - GlobalSection(NestedProjects) = preSolution - {3CAF09A6-6ABD-41D9-BA57-9A822B8095F7} = {490FAE47-4476-4508-B216-505FC850447F} - {2B65466D-D4F8-48FE-A516-014404414897} = {490FAE47-4476-4508-B216-505FC850447F} - {E0FDBFA9-AE5F-42FB-8A60-4E5D9E7D5414} = {62AAE0FE-4099-4A48-AA3C-F76F14C62655} - {37E11FFD-14D7-41B4-B04C-2950206C64DB} = {62AAE0FE-4099-4A48-AA3C-F76F14C62655} - {FB92A4BE-A43E-4755-8132-EC38E9650B80} = {679E6084-0C45-4807-BFEE-D2FDA44B2188} - {73B802A7-3DDE-4B02-9E3D-D6B9EE42DE5D} = {679E6084-0C45-4807-BFEE-D2FDA44B2188} - {7EE133B6-5A02-41B7-9D89-41D9EA14184E} = {CC2C88C0-65F2-445D-BE78-973B840FE281} - {D48E5EC2-CF83-4B17-8BBA-BDE52ADFAB1F} = {EDF8869A-C1E1-491B-BC9F-4A33F4DE1C73} - {10308339-FF5C-43AB-9208-627B6E2FBBDB} = {490FAE47-4476-4508-B216-505FC850447F} - {954C4CA2-9CF4-4C2C-8DE6-180DD8202E38} = {62AAE0FE-4099-4A48-AA3C-F76F14C62655} - {0CF88F0B-6CCB-49B5-B41D-CDC193B51581} = {679E6084-0C45-4807-BFEE-D2FDA44B2188} - {C8FAB96F-F13E-4094-883C-2D38D39EE4A3} = {CC2C88C0-65F2-445D-BE78-973B840FE281} - {C34FCF48-1A98-4268-BFEE-6C9BFC7FD539} = {EDF8869A-C1E1-491B-BC9F-4A33F4DE1C73} - EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {3CAF09A6-6ABD-41D9-BA57-9A822B8095F7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {3CAF09A6-6ABD-41D9-BA57-9A822B8095F7}.Debug|Any CPU.Build.0 = Debug|Any CPU @@ -109,5 +99,31 @@ Global {C34FCF48-1A98-4268-BFEE-6C9BFC7FD539}.Debug|Any CPU.Build.0 = Debug|Any CPU {C34FCF48-1A98-4268-BFEE-6C9BFC7FD539}.Release|Any CPU.ActiveCfg = Release|Any CPU {C34FCF48-1A98-4268-BFEE-6C9BFC7FD539}.Release|Any CPU.Build.0 = Release|Any CPU + {90675788-D5C3-415A-9C18-FF159A75B4D5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {90675788-D5C3-415A-9C18-FF159A75B4D5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {90675788-D5C3-415A-9C18-FF159A75B4D5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {90675788-D5C3-415A-9C18-FF159A75B4D5}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {3CAF09A6-6ABD-41D9-BA57-9A822B8095F7} = {490FAE47-4476-4508-B216-505FC850447F} + {2B65466D-D4F8-48FE-A516-014404414897} = {490FAE47-4476-4508-B216-505FC850447F} + {E0FDBFA9-AE5F-42FB-8A60-4E5D9E7D5414} = {62AAE0FE-4099-4A48-AA3C-F76F14C62655} + {37E11FFD-14D7-41B4-B04C-2950206C64DB} = {62AAE0FE-4099-4A48-AA3C-F76F14C62655} + {FB92A4BE-A43E-4755-8132-EC38E9650B80} = {679E6084-0C45-4807-BFEE-D2FDA44B2188} + {73B802A7-3DDE-4B02-9E3D-D6B9EE42DE5D} = {679E6084-0C45-4807-BFEE-D2FDA44B2188} + {7EE133B6-5A02-41B7-9D89-41D9EA14184E} = {CC2C88C0-65F2-445D-BE78-973B840FE281} + {D48E5EC2-CF83-4B17-8BBA-BDE52ADFAB1F} = {EDF8869A-C1E1-491B-BC9F-4A33F4DE1C73} + {10308339-FF5C-43AB-9208-627B6E2FBBDB} = {490FAE47-4476-4508-B216-505FC850447F} + {954C4CA2-9CF4-4C2C-8DE6-180DD8202E38} = {62AAE0FE-4099-4A48-AA3C-F76F14C62655} + {0CF88F0B-6CCB-49B5-B41D-CDC193B51581} = {679E6084-0C45-4807-BFEE-D2FDA44B2188} + {C8FAB96F-F13E-4094-883C-2D38D39EE4A3} = {CC2C88C0-65F2-445D-BE78-973B840FE281} + {C34FCF48-1A98-4268-BFEE-6C9BFC7FD539} = {EDF8869A-C1E1-491B-BC9F-4A33F4DE1C73} + {90675788-D5C3-415A-9C18-FF159A75B4D5} = {EDF8869A-C1E1-491B-BC9F-4A33F4DE1C73} + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {8C07A667-E8B4-43C7-8053-721584BAD291} EndGlobalSection EndGlobal diff --git a/nuget-publish.bat b/nuget-publish.bat index 1c62ea58..e91089bc 100644 --- a/nuget-publish.bat +++ b/nuget-publish.bat @@ -1,8 +1,8 @@ :start ::定义版本 -set EFCORE2=2.1.0.4 -set EFCORE3=3.1.0.4 -set EFCORE5=5.1.0.4 +set EFCORE2=2.1.0.5 +set EFCORE3=3.1.0.5 +set EFCORE5=5.1.0.5 ::删除所有bin与obj下的文件 @echo off diff --git a/samples/Sample.MySql/Controllers/WeatherForecastController.cs b/samples/Sample.MySql/Controllers/WeatherForecastController.cs new file mode 100644 index 00000000..59dffd1d --- /dev/null +++ b/samples/Sample.MySql/Controllers/WeatherForecastController.cs @@ -0,0 +1,34 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; +using Sample.MySql.Domain.Entities; +using ShardingCore.DbContexts.VirtualDbContexts; +using ShardingCore.Extensions; + +namespace Sample.MySql.Controllers +{ + [ApiController] + [Route("[controller]")] + public class WeatherForecastController : ControllerBase + { + + private readonly IVirtualDbContext _virtualDbContext; + + public WeatherForecastController(IVirtualDbContext virtualDbContext) + { + _virtualDbContext = virtualDbContext; + } + + [HttpGet] + public async Task Get() + { + var result = await _virtualDbContext.Set().AnyAsync(); + var result1 = await _virtualDbContext.Set().Where(o => o.Id == "2" || o.Id == "3").ToShardingListAsync(); + return Ok(result1); + } + } +} diff --git a/samples/Sample.MySql/DIExtension.cs b/samples/Sample.MySql/DIExtension.cs new file mode 100644 index 00000000..435f5467 --- /dev/null +++ b/samples/Sample.MySql/DIExtension.cs @@ -0,0 +1,52 @@ +using System.Collections.Generic; +using System.Linq; +using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.DependencyInjection; +using Sample.MySql.Domain.Entities; +using ShardingCore; +using ShardingCore.DbContexts.VirtualDbContexts; +using ShardingCore.Extensions; + +namespace Sample.MySql +{ +/* +* @Author: xjm +* @Description: +* @Date: Tuesday, 26 January 2021 12:29:04 +* @Email: 326308290@qq.com +*/ + public static class DIExtension + { + public static IApplicationBuilder UseShardingCore(this IApplicationBuilder app) + { + var shardingBootstrapper = app.ApplicationServices.GetRequiredService(); + shardingBootstrapper.Start(); + return app; + } + + public static void DbSeed(this IApplicationBuilder app) + { + using (var scope=app.ApplicationServices.CreateScope()) + { + var virtualDbContext =scope.ServiceProvider.GetService(); + if (!virtualDbContext.Set().ShardingAny()) + { + var ids = Enumerable.Range(1, 1000); + var userMods = new List(); + foreach (var id in ids) + { + userMods.Add(new SysUserMod() + { + Id = id.ToString(), + Age = id, + Name = $"name_{id}", + }); + } + + virtualDbContext.InsertRange(userMods); + virtualDbContext.SaveChanges(); + } + } + } + } +} \ No newline at end of file diff --git a/samples/Sample.MySql/DbContexts/DefaultTableDbContext.cs b/samples/Sample.MySql/DbContexts/DefaultTableDbContext.cs new file mode 100644 index 00000000..55ce47e7 --- /dev/null +++ b/samples/Sample.MySql/DbContexts/DefaultTableDbContext.cs @@ -0,0 +1,23 @@ +using Microsoft.EntityFrameworkCore; +using Sample.MySql.Domain.Maps; +using ShardingCore.DbContexts.ShardingDbContexts; + +namespace Sample.MySql.DbContexts +{ + public class DefaultTableDbContext: DbContext,IShardingTableDbContext + { + public DefaultTableDbContext(ShardingDbContextOptions shardingDbContextOptions):base(shardingDbContextOptions.DbContextOptions) + { + + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + base.OnModelCreating(modelBuilder); + modelBuilder.ApplyConfiguration(new SysUserModMap()); + modelBuilder.ApplyConfiguration(new SysTestMap()); + } + + public string ModelChangeKey { get; set; } + } +} diff --git a/samples/Sample.MySql/Domain/Entities/SysTest.cs b/samples/Sample.MySql/Domain/Entities/SysTest.cs new file mode 100644 index 00000000..0c1a6e88 --- /dev/null +++ b/samples/Sample.MySql/Domain/Entities/SysTest.cs @@ -0,0 +1,8 @@ +namespace Sample.MySql.Domain.Entities +{ + public class SysTest + { + public string Id { get; set; } + public string UserId { get; set; } + } +} \ No newline at end of file diff --git a/samples/Sample.MySql/Domain/Entities/SysUserMod.cs b/samples/Sample.MySql/Domain/Entities/SysUserMod.cs new file mode 100644 index 00000000..dec9cb46 --- /dev/null +++ b/samples/Sample.MySql/Domain/Entities/SysUserMod.cs @@ -0,0 +1,27 @@ +using ShardingCore.Core; + +namespace Sample.MySql.Domain.Entities +{ +/* +* @Author: xjm +* @Description: +* @Date: Tuesday, 26 January 2021 12:25:39 +* @Email: 326308290@qq.com +*/ + public class SysUserMod:IShardingTable + { + /// + /// 用户Id用于分表 + /// + [ShardingTableKey] + public string Id { get; set; } + /// + /// 用户名称 + /// + public string Name { get; set; } + /// + /// 用户姓名 + /// + public int Age { get; set; } + } +} \ No newline at end of file diff --git a/samples/Sample.MySql/Domain/Maps/SysTestMap.cs b/samples/Sample.MySql/Domain/Maps/SysTestMap.cs new file mode 100644 index 00000000..16af9c9d --- /dev/null +++ b/samples/Sample.MySql/Domain/Maps/SysTestMap.cs @@ -0,0 +1,17 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; +using Sample.MySql.Domain.Entities; + +namespace Sample.MySql.Domain.Maps +{ + public class SysTestMap:IEntityTypeConfiguration + { + public void Configure(EntityTypeBuilder builder) + { + builder.HasKey(o => o.Id); + builder.Property(o => o.Id).IsRequired().HasMaxLength(128); + builder.Property(o => o.UserId).IsRequired().HasMaxLength(128); + builder.ToTable(nameof(SysTest)); + } + } +} \ No newline at end of file diff --git a/samples/Sample.MySql/Domain/Maps/SysUserModMap.cs b/samples/Sample.MySql/Domain/Maps/SysUserModMap.cs new file mode 100644 index 00000000..0d629c94 --- /dev/null +++ b/samples/Sample.MySql/Domain/Maps/SysUserModMap.cs @@ -0,0 +1,23 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; +using Sample.MySql.Domain.Entities; + +namespace Sample.MySql.Domain.Maps +{ +/* +* @Author: xjm +* @Description: +* @Date: Thursday, 14 January 2021 15:37:33 +* @Email: 326308290@qq.com +*/ + public class SysUserModMap:IEntityTypeConfiguration + { + public void Configure(EntityTypeBuilder builder) + { + builder.HasKey(o => o.Id); + builder.Property(o => o.Id).IsRequired().HasMaxLength(128); + builder.Property(o => o.Name).HasMaxLength(128); + builder.ToTable(nameof(SysUserMod)); + } + } +} \ No newline at end of file diff --git a/samples/Sample.MySql/Program.cs b/samples/Sample.MySql/Program.cs new file mode 100644 index 00000000..b7b33031 --- /dev/null +++ b/samples/Sample.MySql/Program.cs @@ -0,0 +1,26 @@ +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Sample.MySql +{ + public class Program + { + public static void Main(string[] args) + { + CreateHostBuilder(args).Build().Run(); + } + + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + }); + } +} diff --git a/samples/Sample.MySql/Properties/launchSettings.json b/samples/Sample.MySql/Properties/launchSettings.json new file mode 100644 index 00000000..82b6a434 --- /dev/null +++ b/samples/Sample.MySql/Properties/launchSettings.json @@ -0,0 +1,30 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:53082", + "sslPort": 0 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "weatherforecast", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "Sample.MySql": { + "commandName": "Project", + "launchBrowser": true, + "launchUrl": "weatherforecast", + "applicationUrl": "http://localhost:5000", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/samples/Sample.MySql/Sample.MySql.csproj b/samples/Sample.MySql/Sample.MySql.csproj new file mode 100644 index 00000000..77acc5d1 --- /dev/null +++ b/samples/Sample.MySql/Sample.MySql.csproj @@ -0,0 +1,12 @@ + + + + netcoreapp3.1 + + + + + + + + diff --git a/samples/Sample.MySql/Shardings/SysUserModVirtualTableRoute.cs b/samples/Sample.MySql/Shardings/SysUserModVirtualTableRoute.cs new file mode 100644 index 00000000..e9802f31 --- /dev/null +++ b/samples/Sample.MySql/Shardings/SysUserModVirtualTableRoute.cs @@ -0,0 +1,18 @@ +using Sample.MySql.Domain.Entities; +using ShardingCore.VirtualRoutes.Mods; + +namespace Sample.MySql.Shardings +{ +/* +* @Author: xjm +* @Description: +* @Date: Thursday, 14 January 2021 15:39:27 +* @Email: 326308290@qq.com +*/ + public class SysUserModVirtualTableRoute : AbstractSimpleShardingModKeyStringVirtualTableRoute + { + public SysUserModVirtualTableRoute() : base(2,3) + { + } + } +} \ No newline at end of file diff --git a/samples/Sample.MySql/Startup.cs b/samples/Sample.MySql/Startup.cs new file mode 100644 index 00000000..83c84747 --- /dev/null +++ b/samples/Sample.MySql/Startup.cs @@ -0,0 +1,64 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Sample.MySql.DbContexts; +using Sample.MySql.Shardings; +using ShardingCore.MySql; + +namespace Sample.MySql +{ + public class Startup + { + public Startup(IConfiguration configuration) + { + Configuration = configuration; + } + + public IConfiguration Configuration { get; } + + // This method gets called by the runtime. Use this method to add services to the container. + public void ConfigureServices(IServiceCollection services) + { + services.AddControllers(); + services.AddShardingMySql(o => + { + o.EnsureCreatedWithOutShardingTable = true; + o.CreateShardingTableOnStart = true; + o.AddShardingDbContextWithShardingTable("conn1", "server=106.54.131.109;userid=xjm;password=mysqlXJM#qwe;database=sharding_db123;Charset=utf8;Allow Zero Datetime=True; Pooling=true; Max Pool Size=512;sslmode=none;Allow User Variables=True;", dbConfig => + { + dbConfig.AddShardingTableRoute(); + }); + //o.AddDataSourceVirtualRoute<>(); + + }); + } + + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + { + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + } + + app.UseShardingCore(); + app.UseRouting(); + + app.UseAuthorization(); + + app.UseEndpoints(endpoints => + { + endpoints.MapControllers(); + }); + app.DbSeed(); + } + } +} diff --git a/samples/Sample.MySql/WeatherForecast.cs b/samples/Sample.MySql/WeatherForecast.cs new file mode 100644 index 00000000..7b17b5c8 --- /dev/null +++ b/samples/Sample.MySql/WeatherForecast.cs @@ -0,0 +1,15 @@ +using System; + +namespace Sample.MySql +{ + public class WeatherForecast + { + public DateTime Date { get; set; } + + public int TemperatureC { get; set; } + + public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); + + public string Summary { get; set; } + } +} diff --git a/samples/Sample.MySql/appsettings.Development.json b/samples/Sample.MySql/appsettings.Development.json new file mode 100644 index 00000000..8983e0fc --- /dev/null +++ b/samples/Sample.MySql/appsettings.Development.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git a/samples/Sample.MySql/appsettings.json b/samples/Sample.MySql/appsettings.json new file mode 100644 index 00000000..d9d9a9bf --- /dev/null +++ b/samples/Sample.MySql/appsettings.json @@ -0,0 +1,10 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "AllowedHosts": "*" +} diff --git a/src/ShardingCore.MySql/DIExtension.cs b/src/ShardingCore.MySql/DIExtension.cs index 7a02cd40..12cf4cce 100644 --- a/src/ShardingCore.MySql/DIExtension.cs +++ b/src/ShardingCore.MySql/DIExtension.cs @@ -36,6 +36,7 @@ namespace ShardingCore.MySql var options = new MySqlOptions(); configure(options); + services.AddSingleton(options); services.AddSingleton(sp => options); services.AddShardingCore(); diff --git a/src/ShardingCore.MySql/EFCores/ShardingMySqlQuerySqlGenerator.cs b/src/ShardingCore.MySql/EFCores/ShardingMySqlQuerySqlGenerator.cs index d98dc341..fd5bfa0c 100644 --- a/src/ShardingCore.MySql/EFCores/ShardingMySqlQuerySqlGenerator.cs +++ b/src/ShardingCore.MySql/EFCores/ShardingMySqlQuerySqlGenerator.cs @@ -179,7 +179,7 @@ namespace ShardingCore.MySql.EFCores var tails = shardingAccessor.ShardingContext.GetContextQueryTails(virtualTable); var tailPrefix = virtualTable.ShardingConfig.TailPrefix; string newTableName = null; - var sqlGenerationHelper = typeof(DefaultQuerySqlGenerator).GetTypeFieldValue(this, "_sqlGenerationHelper") as ISqlGenerationHelper; + var sqlGenerationHelper = typeof(DefaultQuerySqlGenerator).GetTypePropertyValue(this, "SqlGenerator") as ISqlGenerationHelper; if (tails.IsEmpty()) { diff --git a/src/ShardingCore/Core/Internal/StreamMerge/Enumerators/StreamMergeAsyncEnumerator.cs b/src/ShardingCore/Core/Internal/StreamMerge/Enumerators/StreamMergeAsyncEnumerator.cs index ea9faa98..8a00f638 100644 --- a/src/ShardingCore/Core/Internal/StreamMerge/Enumerators/StreamMergeAsyncEnumerator.cs +++ b/src/ShardingCore/Core/Internal/StreamMerge/Enumerators/StreamMergeAsyncEnumerator.cs @@ -47,13 +47,18 @@ namespace ShardingCore.Core.Internal.StreamMerge.Enumerators } return await _source.MoveNext(); } + public async Task MoveNext(CancellationToken cancellationToken) + { + if (skip) + { + skip = false; + return null != _source.Current; + } + return await _source.MoveNext(cancellationToken); + } #endif - public Task MoveNext(CancellationToken cancellationToken) - { - throw new NotImplementedException(); - } public T Current => skip?default:_source.Current; diff --git a/src/ShardingCore/Extensions/DbContextExtension.cs b/src/ShardingCore/Extensions/DbContextExtension.cs index e87f1ffc..90c933cd 100644 --- a/src/ShardingCore/Extensions/DbContextExtension.cs +++ b/src/ShardingCore/Extensions/DbContextExtension.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Linq; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Infrastructure.Internal; using Microsoft.EntityFrameworkCore.Internal; using Microsoft.EntityFrameworkCore.Metadata; using Microsoft.EntityFrameworkCore.Metadata.Internal; @@ -111,13 +112,12 @@ var contextModelRelationalModel = contextModel.RelationalModel as RelationalMode #if EFCORE2 var modelSource = serviceScope.ServiceProvider.GetService(); - var modelSourceImpl = modelSource as ModelSource; + var modelSourceImpl = modelSource as RelationalModelSource; var modelSourceDependencies = modelSourceImpl.GetPropertyValue("Dependencies") as ModelSourceDependencies; - var models = - modelSourceImpl.GetPropertyValue("_models") as ConcurrentDictionary>; + typeof(ModelSource).GetTypeFieldValue(modelSourceImpl, "_models") as ConcurrentDictionary>; object key1 = modelSourceDependencies.ModelCacheKeyFactory.Create(dbContext); models.TryRemove(key1,out var del); #endif @@ -138,14 +138,20 @@ var contextModelRelationalModel = contextModel.RelationalModel as RelationalMode var syncObject = dependenciesModelSource.GetFieldValue("_syncObject"); return syncObject; #endif -#if !EFCORE5 +#if EFCORE3 var modelSource = serviceScope.ServiceProvider.GetService(); var modelSourceImpl = modelSource as ModelSource; var syncObject = modelSourceImpl.GetFieldValue("_syncObject"); return syncObject; #endif +#if EFCORE2 + return sLock; +#endif } + + private static object sLock = new object(); + } } \ No newline at end of file diff --git a/src/ShardingCore/ShardingBootstrapper.cs b/src/ShardingCore/ShardingBootstrapper.cs index ea7fa0cb..0d5557a2 100644 --- a/src/ShardingCore/ShardingBootstrapper.cs +++ b/src/ShardingCore/ShardingBootstrapper.cs @@ -188,6 +188,7 @@ namespace ShardingCore new ShardingDbContextOptions( dbContextOptionsProvider.GetDbContextOptions(shardingConfig.ConnectKey), string.Empty),scope.ServiceProvider); var modelCacheSyncObject = context.GetModelCacheSyncObject(); + lock (modelCacheSyncObject) { context.RemoveDbContextRelationModelThatIsShardingTable();