提交并发布x.6.0.6

This commit is contained in:
xuejiaming 2022-07-09 14:27:03 +08:00
parent 86455805f0
commit 3699ccf426
15 changed files with 250 additions and 22 deletions

View File

@ -65,6 +65,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample.AutoCreateIfPresent"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ShardingCore.CommonTest", "test\ShardingCore.CommonTest\ShardingCore.CommonTest.csproj", "{3E895438-E609-4860-8391-6897ED55DA06}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample.ShardingConsole", "samples\Sample.ShardingConsole\Sample.ShardingConsole.csproj", "{BAB101D4-2BFF-44CE-90E3-8B72DDB266B8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -167,6 +169,10 @@ Global
{3E895438-E609-4860-8391-6897ED55DA06}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3E895438-E609-4860-8391-6897ED55DA06}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3E895438-E609-4860-8391-6897ED55DA06}.Release|Any CPU.Build.0 = Release|Any CPU
{BAB101D4-2BFF-44CE-90E3-8B72DDB266B8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BAB101D4-2BFF-44CE-90E3-8B72DDB266B8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BAB101D4-2BFF-44CE-90E3-8B72DDB266B8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BAB101D4-2BFF-44CE-90E3-8B72DDB266B8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -196,6 +202,7 @@ Global
{D839D632-4AE4-4F75-8A2C-49EE029B0787} = {EDF8869A-C1E1-491B-BC9F-4A33F4DE1C73}
{40C83D48-0614-4651-98C6-2ABBE857D83D} = {EDF8869A-C1E1-491B-BC9F-4A33F4DE1C73}
{3E895438-E609-4860-8391-6897ED55DA06} = {CC2C88C0-65F2-445D-BE78-973B840FE281}
{BAB101D4-2BFF-44CE-90E3-8B72DDB266B8} = {EDF8869A-C1E1-491B-BC9F-4A33F4DE1C73}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {8C07A667-E8B4-43C7-8053-721584BAD291}

View File

@ -1,9 +1,9 @@
:start
::定义版本
set EFCORE2=2.6.0.5
set EFCORE3=3.6.0.5
set EFCORE5=5.6.0.5
set EFCORE6=6.6.0.5
set EFCORE2=2.6.0.6
set EFCORE3=3.6.0.6
set EFCORE5=5.6.0.6
set EFCORE6=6.6.0.6
::删除所有bin与obj下的文件
@echo off

View File

@ -71,7 +71,8 @@ namespace Sample.MySql
o.AddShardingTableRoute<SysUserModVirtualTableRoute>();
o.AddShardingDataSourceRoute<SysUserModVirtualDataSourceRoute>();
}).UseConfig(o =>
{
{
o.ThrowIfQueryRouteNotMatch = false;
o.UseShardingQuery((conStr,builder)=>
{
builder.UseMySql(conStr, new MySqlServerVersion(new Version()))

View File

@ -0,0 +1,31 @@
using Microsoft.EntityFrameworkCore;
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
using ShardingCore.Sharding;
using ShardingCore.Sharding.Abstractions;
namespace Sample.ShardingConsole;
public class MyDbContext:AbstractShardingDbContext,IShardingTableDbContext
{
public MyDbContext(DbContextOptions<MyDbContext> options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Order>(entity =>
{
entity.HasKey(o => o.Id);
entity.Property(o => o.Id).IsRequired().IsUnicode(false).HasMaxLength(50);
entity.Property(o=>o.Payer).IsRequired().IsUnicode(false).HasMaxLength(50);
entity.Property(o => o.Area).IsRequired().IsUnicode(false).HasMaxLength(50);
entity.Property(o => o.OrderStatus).HasConversion<int>();
entity.ToTable(nameof(Order));
});
}
/// <summary>
/// empty impl if use sharding table
/// </summary>
public IRouteTail RouteTail { get; set; }
}

View File

@ -0,0 +1,16 @@
using Microsoft.EntityFrameworkCore;
using ShardingCore;
using ShardingCore.Core.DbContextCreator;
using ShardingCore.Core.ServiceProviders;
namespace Sample.ShardingConsole;
public class MyDbContextCreator:ActivatorDbContextCreator<MyDbContext>
{
public override DbContext GetShellDbContext(IShardingProvider shardingProvider)
{
var dbContextOptionsBuilder = new DbContextOptionsBuilder<MyDbContext>();
dbContextOptionsBuilder.UseDefaultSharding<MyDbContext>(ShardingProvider.ShardingRuntimeContext);
return new MyDbContext(dbContextOptionsBuilder.Options);
}
}

View File

@ -0,0 +1,40 @@
namespace Sample.ShardingConsole;
/// <summary>
/// order table
/// </summary>
public class Order
{
/// <summary>
/// order Id
/// </summary>
public string Id { get; set; }
/// <summary>
/// payer id
/// </summary>
public string Payer { get; set; }
/// <summary>
/// pay money cent
/// </summary>
public long Money { get; set; }
/// <summary>
/// area
/// </summary>
public string Area { get; set; }
/// <summary>
/// order status
/// </summary>
public OrderStatusEnum OrderStatus { get; set; }
/// <summary>
/// CreationTime
/// </summary>
public DateTime CreationTime { get; set; }
}
public enum OrderStatusEnum
{
NoPay=1,
Paying=2,
Payed=3,
PayFail=4
}

View File

@ -0,0 +1,18 @@
using ShardingCore.Core.EntityMetadatas;
using ShardingCore.VirtualRoutes.Mods;
namespace Sample.ShardingConsole;
public class OrderVirtualTableRoute:AbstractSimpleShardingModKeyStringVirtualTableRoute<Order>
{
public OrderVirtualTableRoute() : base(2, 3)
{
}
public override void Configure(EntityMetadataTableBuilder<Order> builder)
{
builder.ShardingProperty(o => o.Id);
builder.AutoCreateTable(null);
builder.TableSeparator("_");
}
}

View File

@ -0,0 +1,27 @@
// See https://aka.ms/new-console-template for more information
using Microsoft.EntityFrameworkCore;
using Sample.ShardingConsole;
using ShardingCore;
using ShardingCore.Extensions;
ShardingProvider.ShardingRuntimeContext.UseAutoShardingCreate();
ShardingProvider.ShardingRuntimeContext.UseAutoTryCompensateTable();
var dbContextOptionsBuilder = new DbContextOptionsBuilder<MyDbContext>();
dbContextOptionsBuilder.UseDefaultSharding<MyDbContext>(ShardingProvider.ShardingRuntimeContext);
using (var dbcontext = new MyDbContext(dbContextOptionsBuilder.Options))
{
dbcontext.Add(new Order()
{
Id = Guid.NewGuid().ToString("n"),
Payer = "111",
Area = "123",
OrderStatus = OrderStatusEnum.Payed,
Money = 100,
CreationTime = DateTime.Now
});
dbcontext.SaveChanges();
}
Console.WriteLine("Hello, World!");

View File

@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\ShardingCore\ShardingCore.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="6.0.0" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="6.0.1" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,40 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using ShardingCore;
using ShardingCore.Core.DbContextCreator;
using ShardingCore.Core.RuntimeContexts;
namespace Sample.ShardingConsole;
public class ShardingProvider
{
private static ILoggerFactory efLogger = LoggerFactory.Create(builder =>
{
builder.AddFilter((category, level) => category == DbLoggerCategory.Database.Command.Name && level == LogLevel.Information).AddConsole();
});
private static readonly IShardingRuntimeContext instance;
public static IShardingRuntimeContext ShardingRuntimeContext => instance;
static ShardingProvider()
{
instance=new ShardingRuntimeBuilder<MyDbContext>().UseRouteConfig(op =>
{
op.AddShardingTableRoute<OrderVirtualTableRoute>();
})
.UseConfig((sp,op) =>
{
op.UseShardingQuery((con, b) =>
{
b.UseMySql(con, new MySqlServerVersion(new Version()))
.UseLoggerFactory(efLogger);
});
op.UseShardingTransaction((con, b) =>
{
b.UseMySql(con, new MySqlServerVersion(new Version()))
.UseLoggerFactory(efLogger);
});
op.AddDefaultDataSource("ds0", "server=127.0.0.1;port=3306;database=console0;userid=root;password=root;");
}).ReplaceService<IDbContextCreator, MyDbContextCreator>(ServiceLifetime.Singleton).Build();
}
}

View File

@ -6,25 +6,36 @@ using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using ShardingCore.Exceptions;
using ShardingCore.Sharding.Abstractions;
namespace ShardingCore.EFCores
{
//public class ShardingChangeTracker: ChangeTracker
//{
// private readonly ICurrentDbContextDiscover _contextDiscover;
public class ShardingChangeTracker: ChangeTracker
{
private readonly ICurrentDbContextDiscover _contextDiscover;
// public ShardingChangeTracker(DbContext context, IStateManager stateManager, IChangeDetector changeDetector, IModel model, IEntityEntryGraphIterator graphIterator) : base(context, stateManager, changeDetector, model, graphIterator)
// {
// _contextDiscover = context as ICurrentDbContextDiscover?? throw new ShardingCoreNotSupportException($"{context.GetType()} not impl {nameof(ICurrentDbContextDiscover)}");
// }
public ShardingChangeTracker(DbContext context, IStateManager stateManager, IChangeDetector changeDetector, IModel model, IEntityEntryGraphIterator graphIterator) : base(context, stateManager, changeDetector, model, graphIterator)
{
_contextDiscover = context as ICurrentDbContextDiscover?? throw new ShardingCoreNotSupportException($"{context.GetType()} not impl {nameof(ICurrentDbContextDiscover)}");
}
// public override bool HasChanges()
// {
// return _contextDiscover.GetCurrentDbContexts().Any(o =>
// o.Value.GetCurrentContexts().Any(r => r.Value.ChangeTracker.HasChanges()));
// }
//}
public override bool HasChanges()
{
return _contextDiscover.GetCurrentDbContexts().Any(o =>
o.Value.GetCurrentContexts().Any(r => r.Value.ChangeTracker.HasChanges()));
}
public override IEnumerable<EntityEntry> Entries()
{
return _contextDiscover.GetCurrentDbContexts().SelectMany(o => o.Value.GetCurrentContexts().SelectMany(cd=>cd.Value.ChangeTracker.Entries()));
}
public override IEnumerable<EntityEntry<TEntity>> Entries<TEntity>()
{
return _contextDiscover.GetCurrentDbContexts().SelectMany(o => o.Value.GetCurrentContexts().SelectMany(cd=>cd.Value.ChangeTracker.Entries<TEntity>()));
}
}
}

View File

@ -10,7 +10,11 @@ namespace ShardingCore.Extensions
public static class ShardingRuntimeExtension
{
public static void UseAutoShardingCreate(this IShardingRuntimeContext shardingRuntimeContext)
{
shardingRuntimeContext.CheckRequirement();
shardingRuntimeContext.AutoShardingCreate();
}
/// <summary>
/// 自动尝试补偿表
/// </summary>

View File

@ -0,0 +1,10 @@
using System.Collections.Generic;
using ShardingCore.Sharding.ShardingDbContextExecutors;
namespace ShardingCore.Sharding.Abstractions
{
public interface ICurrentDbContextDiscover
{
IDictionary<string, IDataSourceDbContext> GetCurrentDbContexts();
}
}

View File

@ -99,6 +99,11 @@ namespace ShardingCore
return new ShardingCoreConfigBuilder<TShardingDbContext>(services);
}
public static void UseDefaultSharding<TShardingDbContext>(this DbContextOptionsBuilder dbContextOptionsBuilder,IServiceProvider serviceProvider) where TShardingDbContext : DbContext, IShardingDbContext
{
var shardingRuntimeContext = serviceProvider.GetRequiredService<IShardingRuntimeContext>();
dbContextOptionsBuilder.UseDefaultSharding<TShardingDbContext>(shardingRuntimeContext);
}
public static void UseDefaultSharding<TShardingDbContext>(IServiceProvider serviceProvider,
DbContextOptionsBuilder dbContextOptionsBuilder) where TShardingDbContext : DbContext, IShardingDbContext
{
@ -254,8 +259,7 @@ namespace ShardingCore
public static void UseAutoShardingCreate(this IServiceProvider serviceProvider)
{
var shardingRuntimeContext = serviceProvider.GetRequiredService<IShardingRuntimeContext>();
shardingRuntimeContext.CheckRequirement();
shardingRuntimeContext.AutoShardingCreate();
shardingRuntimeContext.UseAutoShardingCreate();
}
/// <summary>

View File

@ -83,7 +83,7 @@ namespace ShardingCore.Test3x
{
"A", new HashSet<string>()
{
"Data Source=localhost;Initial Catalog=ShardingCoreDBB;Integrated Security=True;"
"Data Source=localhost;Initial Catalog=ShardingCoreDBB;Integrated Security=True;",
}
}
};