Skip to content

Commit

Permalink
[#283],[#284]支持efcore9
Browse files Browse the repository at this point in the history
  • Loading branch information
xuejmnet committed Dec 28, 2024
1 parent 8e75aa8 commit 8be1473
Show file tree
Hide file tree
Showing 76 changed files with 5,403 additions and 57 deletions.
16 changes: 16 additions & 0 deletions ShardingCore.sln
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src7", "src7", "{A5321A3E-8
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ShardingCore.Test7x", "test\ShardingCore.Test7x\ShardingCore.Test7x.csproj", "{FC6C1017-69D5-44E0-8C4C-99B6F476FB55}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src8", "src8", "{292AC39B-A187-4C59-80D6-AC3B4D823C46}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ShardingCore8", "src8\ShardingCore8\ShardingCore8.csproj", "{226D717B-561A-4ABC-8A13-7962E6500B15}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ShardingCore.Test8x", "test\ShardingCore.Test8x\ShardingCore.Test8x.csproj", "{DED01313-FF99-4500-AB71-BA2E406146B8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -207,6 +213,14 @@ Global
{FC6C1017-69D5-44E0-8C4C-99B6F476FB55}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FC6C1017-69D5-44E0-8C4C-99B6F476FB55}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FC6C1017-69D5-44E0-8C4C-99B6F476FB55}.Release|Any CPU.Build.0 = Release|Any CPU
{226D717B-561A-4ABC-8A13-7962E6500B15}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{226D717B-561A-4ABC-8A13-7962E6500B15}.Debug|Any CPU.Build.0 = Debug|Any CPU
{226D717B-561A-4ABC-8A13-7962E6500B15}.Release|Any CPU.ActiveCfg = Release|Any CPU
{226D717B-561A-4ABC-8A13-7962E6500B15}.Release|Any CPU.Build.0 = Release|Any CPU
{DED01313-FF99-4500-AB71-BA2E406146B8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DED01313-FF99-4500-AB71-BA2E406146B8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DED01313-FF99-4500-AB71-BA2E406146B8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DED01313-FF99-4500-AB71-BA2E406146B8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -242,6 +256,8 @@ Global
{3B5A4B03-5190-41A8-8E4B-F95A79A5C018} = {EDF8869A-C1E1-491B-BC9F-4A33F4DE1C73}
{A308D253-A072-40EC-AA97-15EE82BB8AAD} = {A5321A3E-8539-4E2A-889C-8E91989EECBD}
{FC6C1017-69D5-44E0-8C4C-99B6F476FB55} = {CC2C88C0-65F2-445D-BE78-973B840FE281}
{226D717B-561A-4ABC-8A13-7962E6500B15} = {292AC39B-A187-4C59-80D6-AC3B4D823C46}
{DED01313-FF99-4500-AB71-BA2E406146B8} = {CC2C88C0-65F2-445D-BE78-973B840FE281}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {8C07A667-E8B4-43C7-8053-721584BAD291}
Expand Down
6 changes: 3 additions & 3 deletions samples/Sample.MySql/Sample.MySql.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

<ItemGroup>
<PackageReference Include="EntityFrameworkCore.UseRowNumberForPaging" Version="0.4.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="8.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.0">
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="9.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="9.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="8.0.0-beta.2" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="9.0.0-preview.2.efcore.9.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
17 changes: 9 additions & 8 deletions samples/Sample.MySql/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,14 +253,15 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
// 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())
// {
// defaultShardingDbContext.Database.GenerateCreateScript();
// }
// }
using (var scope = app.ApplicationServices.CreateScope())
{
var defaultShardingDbContext = scope.ServiceProvider.GetService<DefaultShardingDbContext>();
_ = defaultShardingDbContext.Model;
// if (defaultShardingDbContext.Database.GetPendingMigrations().Any())
// {
// defaultShardingDbContext.Database.GenerateCreateScript();
// }
}

// app.ApplicationServices.UseAutoTryCompensateTable();
// using (var scope = app.ApplicationServices.CreateScope())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if EFCORE7 || EFCORE8
#if EFCORE7 || EFCORE8|| EFCORE9
using Microsoft.EntityFrameworkCore.Migrations;
using ShardingCore.Core.RuntimeContexts;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if EFCORE7 || EFCORE8
#if EFCORE7 || EFCORE8|| EFCORE9
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down
49 changes: 49 additions & 0 deletions src/ShardingCore/EFCores/EFCore7_8_9x/ShardingMigrator9x.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

#if EFCORE9
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Migrations.Internal;
using Microsoft.EntityFrameworkCore.Storage;
using ShardingCore.Core.RuntimeContexts;
using ShardingCore.Extensions;
using ShardingCore.Helpers;

namespace ShardingCore.EFCores
{
public class ShardingMigrator:Migrator
{
private readonly IShardingRuntimeContext _shardingRuntimeContext;

public ShardingMigrator(IShardingRuntimeContext shardingRuntimeContext,IMigrationsAssembly migrationsAssembly, IHistoryRepository historyRepository, IDatabaseCreator databaseCreator, IMigrationsSqlGenerator migrationsSqlGenerator, IRawSqlCommandBuilder rawSqlCommandBuilder, IMigrationCommandExecutor migrationCommandExecutor, IRelationalConnection connection, ISqlGenerationHelper sqlGenerationHelper, ICurrentDbContext currentContext, IModelRuntimeInitializer modelRuntimeInitializer, IDiagnosticsLogger<DbLoggerCategory.Migrations> logger, IRelationalCommandDiagnosticsLogger commandLogger, IDatabaseProvider databaseProvider, IMigrationsModelDiffer migrationsModelDiffer, IDesignTimeModel designTimeModel, IDbContextOptions contextOptions, IExecutionStrategy executionStrategy) : base(migrationsAssembly, historyRepository, databaseCreator, migrationsSqlGenerator, rawSqlCommandBuilder, migrationCommandExecutor, connection, sqlGenerationHelper, currentContext, modelRuntimeInitializer, logger, commandLogger, databaseProvider, migrationsModelDiffer, designTimeModel, contextOptions, executionStrategy)
{
_shardingRuntimeContext = shardingRuntimeContext;
}

public override void Migrate(string targetMigration = null)
{
this.MigrateAsync(targetMigration).WaitAndUnwrapException(false);
// base.Migrate(targetMigration);
}

public override async Task MigrateAsync(string targetMigration = null, CancellationToken cancellationToken = new CancellationToken())
{
var virtualDataSource = _shardingRuntimeContext.GetVirtualDataSource();
var allDataSourceNames = virtualDataSource.GetAllDataSourceNames();
await DynamicShardingHelper.DynamicMigrateWithDataSourcesAsync(_shardingRuntimeContext, allDataSourceNames, null,targetMigration,cancellationToken).ConfigureAwait(false);

}

public override string GenerateScript(string fromMigration = null, string toMigration = null,
MigrationsSqlGenerationOptions options = MigrationsSqlGenerationOptions.Default)
{
return new ScriptMigrationGenerator(_shardingRuntimeContext, fromMigration, toMigration, options).GenerateScript();
}
}

}
#endif
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if EFCORE7 || EFCORE8
#if EFCORE7 || EFCORE8 || EFCORE9
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if EFCORE7 || EFCORE8
#if EFCORE7 || EFCORE8 || EFCORE9

using System;
using System.Collections.Generic;
Expand Down
112 changes: 112 additions & 0 deletions src/ShardingCore/EFCores/EFCore7_8_9x/ShardingStateManager9x.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#if EFCORE9

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage;
using ShardingCore.Exceptions;
using ShardingCore.Sharding.Abstractions;

namespace ShardingCore.EFCores
{
public class ShardingStateManager : StateManager
{
private readonly IShardingDbContext _currentShardingDbContext;

public ShardingStateManager(StateManagerDependencies dependencies) : base(dependencies)
{
_currentShardingDbContext = (IShardingDbContext)Context;
}

public override InternalEntityEntry GetOrCreateEntry(object entity)
{
var genericDbContext = _currentShardingDbContext.GetShardingExecutor().CreateGenericDbContext(entity);
var dbContextDependencies = genericDbContext.GetService<IDbContextDependencies>();
var stateManager = dbContextDependencies.StateManager;
return stateManager.GetOrCreateEntry(entity);
}

public override InternalEntityEntry GetOrCreateEntry(object entity, IEntityType entityType)
{
var genericDbContext = _currentShardingDbContext.GetShardingExecutor().CreateGenericDbContext(entity);
var findEntityType = genericDbContext.Model.FindEntityType(entity.GetType());
var dbContextDependencies = genericDbContext.GetService<IDbContextDependencies>();
var stateManager = dbContextDependencies.StateManager;
return stateManager.GetOrCreateEntry(entity, findEntityType);
}

public override InternalEntityEntry StartTrackingFromQuery(
IEntityType baseEntityType,
object entity,
in ISnapshot snapshot)
{
throw new ShardingCoreNotImplementedException();
}

public override InternalEntityEntry TryGetEntry(object entity, bool throwOnNonUniqueness = true)
{
throw new ShardingCoreNotImplementedException();
}

public override InternalEntityEntry TryGetEntry(object entity, IEntityType entityType,
bool throwOnTypeMismatch = true)
{
throw new ShardingCoreNotImplementedException();
}

public override int SaveChanges(bool acceptAllChangesOnSuccess)
{
//ApplyShardingConcepts();
int i = 0;
//如果是内部开的事务就内部自己消化
if (Context.Database.AutoTransactionsEnabled && Context.Database.CurrentTransaction == null &&
_currentShardingDbContext.GetShardingExecutor().IsMultiDbContext)
{
using (var tran = Context.Database.BeginTransaction())
{
i = _currentShardingDbContext.GetShardingExecutor().SaveChanges(acceptAllChangesOnSuccess);
tran.Commit();
}
}
else
{
i = _currentShardingDbContext.GetShardingExecutor().SaveChanges(acceptAllChangesOnSuccess);
}

return i;
}

public override async Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess,
CancellationToken cancellationToken = new CancellationToken())
{
//ApplyShardingConcepts();
int i = 0;
//如果是内部开的事务就内部自己消化
if (Context.Database.AutoTransactionsEnabled && Context.Database.CurrentTransaction == null &&
_currentShardingDbContext.GetShardingExecutor().IsMultiDbContext)
{
using (var tran = await Context.Database.BeginTransactionAsync(cancellationToken))
{
i = await _currentShardingDbContext.GetShardingExecutor()
.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
await tran.CommitAsync(cancellationToken);
}
}
else
{
i = await _currentShardingDbContext.GetShardingExecutor()
.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
}


return i;
}
}
}
#endif
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if EFCORE7 || EFCORE8
#if EFCORE7 || EFCORE8 || EFCORE9

using System;
using System.Collections.Generic;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if EFCORE7 || EFCORE8
#if EFCORE7 || EFCORE8 || EFCORE9
using System;
using System.Collections.Generic;
using System.Data.Common;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if EFCORE7 || EFCORE8
#if EFCORE7 || EFCORE8 || EFCORE9
using System;
using System.Collections.Generic;
using System.Data.Common;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if EFCORE7 || EFCORE8
#if EFCORE7 || EFCORE8 || EFCORE9
using System;
using System.Collections.Generic;
using System.Data;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if EFCORE7 || EFCORE8
#if EFCORE7 || EFCORE8 || EFCORE9

using System;
using System.Collections.Generic;
Expand Down
Loading

0 comments on commit 8be1473

Please sign in to comment.