forked from dotnet/efcore
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integrate hierarchyid into the EF Core codebase
Resolves efcore/EFCore.SqlServer.HierarchyId#113, resolves efcore/EFCore.SqlServer.HierarchyId#119, resolves dotnet#365
- Loading branch information
Showing
41 changed files
with
1,211 additions
and
1,215 deletions.
There are no files selected for viewing
10 changes: 6 additions & 4 deletions
10
src/EFCore.SqlServer.Abstractions/EFCore.SqlServer.Abstractions.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<AssemblyName>EntityFrameworkCore.SqlServer.HierarchyId.Abstractions</AssemblyName> | ||
<TargetFramework>netstandard2.1</TargetFramework> | ||
<MinClientVersion>3.6</MinClientVersion> | ||
<AssemblyName>Microsoft.EntityFrameworkCore.SqlServer.Abstractions</AssemblyName> | ||
<RootNamespace>Microsoft.EntityFrameworkCore</RootNamespace> | ||
<Description>Common abstractions for using hierarchyid with EF Core</Description> | ||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
<PackageTags>$(PackageTags);SQL Server;HierarchyId</PackageTags> | ||
<ImplicitUsings>true</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="dotMorten.Microsoft.SqlServer.Types" Version="1.5.0" /> | ||
<PackageReference Include="Microsoft.SqlServer.Types" Version="160.1000.6" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Large diffs are not rendered by default.
Oops, something went wrong.
34 changes: 18 additions & 16 deletions
34
src/EFCore.SqlServer.HierarchyId/Design/SqlServerHierarchyIdDesignTimeServices.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,30 @@ | ||
using Microsoft.EntityFrameworkCore.Design; | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.EntityFrameworkCore.Design; | ||
using Microsoft.EntityFrameworkCore.Scaffolding; | ||
using Microsoft.EntityFrameworkCore.SqlServer.Scaffolding; | ||
using Microsoft.EntityFrameworkCore.SqlServer.Storage; | ||
using Microsoft.EntityFrameworkCore.Storage; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Microsoft.EntityFrameworkCore.SqlServer.Design | ||
namespace Microsoft.EntityFrameworkCore.SqlServer.Design; | ||
|
||
/// <summary> | ||
/// Enables configuring design-time services. Tools will automatically discover implementations of this | ||
/// interface that are in the startup assembly. | ||
/// </summary> | ||
public class SqlServerHierarchyIdDesignTimeServices : IDesignTimeServices | ||
{ | ||
/// <summary> | ||
/// Enables configuring design-time services. Tools will automatically discover implementations of this | ||
/// interface that are in the startup assembly. | ||
/// Configures design-time services. Use this method to override the default design-time services with your | ||
/// own implementations. | ||
/// </summary> | ||
public class SqlServerHierarchyIdDesignTimeServices : IDesignTimeServices | ||
/// <param name="serviceCollection"> The design-time service collection. </param> | ||
public virtual void ConfigureDesignTimeServices(IServiceCollection serviceCollection) | ||
{ | ||
/// <summary> | ||
/// Configures design-time services. Use this method to override the default design-time services with your | ||
/// own implementations. | ||
/// </summary> | ||
/// <param name="serviceCollection"> The design-time service collection. </param> | ||
public virtual void ConfigureDesignTimeServices(IServiceCollection serviceCollection) | ||
{ | ||
serviceCollection | ||
.AddSingleton<IRelationalTypeMappingSourcePlugin, SqlServerHierarchyIdTypeMappingSourcePlugin>() | ||
.AddSingleton<IProviderCodeGeneratorPlugin, SqlServerHierarchyIdCodeGeneratorPlugin>(); | ||
} | ||
serviceCollection | ||
.AddSingleton<IRelationalTypeMappingSourcePlugin, SqlServerHierarchyIdTypeMappingSourcePlugin>() | ||
.AddSingleton<IProviderCodeGeneratorPlugin, SqlServerHierarchyIdCodeGeneratorPlugin>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 20 additions & 18 deletions
38
...SqlServer.HierarchyId/Extensions/SqlServerHierarchyIdDbContextOptionsBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,31 @@ | ||
using Microsoft.EntityFrameworkCore.Infrastructure; | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.EntityFrameworkCore.Infrastructure; | ||
using Microsoft.EntityFrameworkCore.SqlServer.Infrastructure; | ||
|
||
namespace Microsoft.EntityFrameworkCore | ||
namespace Microsoft.EntityFrameworkCore; | ||
|
||
/// <summary> | ||
/// HierarchyId specific extension methods for <see cref="SqlServerDbContextOptionsBuilder"/>. | ||
/// </summary> | ||
public static class SqlServerHierarchyIdDbContextOptionsBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// HierarchyId specific extension methods for <see cref="SqlServerDbContextOptionsBuilder"/>. | ||
/// Enable HierarchyId mappings. | ||
/// </summary> | ||
public static class SqlServerHierarchyIdDbContextOptionsBuilderExtensions | ||
/// <param name="optionsBuilder">The builder being used to configure SQL Server.</param> | ||
/// <returns>The options builder so that further configuration can be chained.</returns> | ||
public static SqlServerDbContextOptionsBuilder UseHierarchyId( | ||
this SqlServerDbContextOptionsBuilder optionsBuilder) | ||
{ | ||
/// <summary> | ||
/// Enable HierarchyId mappings. | ||
/// </summary> | ||
/// <param name="optionsBuilder">The builder being used to configure SQL Server.</param> | ||
/// <returns>The options builder so that further configuration can be chained.</returns> | ||
public static SqlServerDbContextOptionsBuilder UseHierarchyId( | ||
this SqlServerDbContextOptionsBuilder optionsBuilder) | ||
{ | ||
var coreOptionsBuilder = ((IRelationalDbContextOptionsBuilderInfrastructure)optionsBuilder).OptionsBuilder; | ||
var coreOptionsBuilder = ((IRelationalDbContextOptionsBuilderInfrastructure)optionsBuilder).OptionsBuilder; | ||
|
||
var extension = coreOptionsBuilder.Options.FindExtension<SqlServerHierarchyIdOptionsExtension>() | ||
?? new SqlServerHierarchyIdOptionsExtension(); | ||
var extension = coreOptionsBuilder.Options.FindExtension<SqlServerHierarchyIdOptionsExtension>() | ||
?? new SqlServerHierarchyIdOptionsExtension(); | ||
|
||
((IDbContextOptionsBuilderInfrastructure)coreOptionsBuilder).AddOrUpdateExtension(extension); | ||
((IDbContextOptionsBuilderInfrastructure)coreOptionsBuilder).AddOrUpdateExtension(extension); | ||
|
||
return optionsBuilder; | ||
} | ||
return optionsBuilder; | ||
} | ||
} |
34 changes: 18 additions & 16 deletions
34
...FCore.SqlServer.HierarchyId/Extensions/SqlServerHierarchyIdServiceCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,31 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.EntityFrameworkCore.Infrastructure; | ||
using Microsoft.EntityFrameworkCore.Query; | ||
using Microsoft.EntityFrameworkCore.SqlServer.Query.ExpressionTranslators; | ||
using Microsoft.EntityFrameworkCore.SqlServer.Storage; | ||
using Microsoft.EntityFrameworkCore.Storage; | ||
|
||
namespace Microsoft.Extensions.DependencyInjection | ||
namespace Microsoft.Extensions.DependencyInjection; | ||
|
||
/// <summary> | ||
/// EntityFrameworkCore.SqlServer.HierarchyId extension methods for <see cref="IServiceCollection"/>. | ||
/// </summary> | ||
public static class SqlServerHierarchyIdServiceCollectionExtensions | ||
{ | ||
/// <summary> | ||
/// EntityFrameworkCore.SqlServer.HierarchyId extension methods for <see cref="IServiceCollection"/>. | ||
/// Adds the services required for HierarchyId support in the SQL Server provider for Entity Framework. | ||
/// </summary> | ||
public static class SqlServerHierarchyIdServiceCollectionExtensions | ||
/// <param name="serviceCollection">The <see cref="IServiceCollection"/> to add services to.</param> | ||
/// <returns>The same service collection so that multiple calls can be chained.</returns> | ||
public static IServiceCollection AddEntityFrameworkSqlServerHierarchyId( | ||
this IServiceCollection serviceCollection) | ||
{ | ||
/// <summary> | ||
/// Adds the services required for HierarchyId support in the SQL Server provider for Entity Framework. | ||
/// </summary> | ||
/// <param name="serviceCollection">The <see cref="IServiceCollection"/> to add services to.</param> | ||
/// <returns>The same service collection so that multiple calls can be chained.</returns> | ||
public static IServiceCollection AddEntityFrameworkSqlServerHierarchyId( | ||
this IServiceCollection serviceCollection) | ||
{ | ||
new EntityFrameworkRelationalServicesBuilder(serviceCollection) | ||
.TryAdd<IMethodCallTranslatorPlugin, SqlServerHierarchyIdMethodCallTranslatorPlugin>() | ||
.TryAdd<IRelationalTypeMappingSourcePlugin, SqlServerHierarchyIdTypeMappingSourcePlugin>(); | ||
new EntityFrameworkRelationalServicesBuilder(serviceCollection) | ||
.TryAdd<IMethodCallTranslatorPlugin, SqlServerHierarchyIdMethodCallTranslatorPlugin>() | ||
.TryAdd<IRelationalTypeMappingSourcePlugin, SqlServerHierarchyIdTypeMappingSourcePlugin>(); | ||
|
||
return serviceCollection; | ||
} | ||
return serviceCollection; | ||
} | ||
} |
77 changes: 38 additions & 39 deletions
77
src/EFCore.SqlServer.HierarchyId/Infrastructure/SqlServerHierarchyIdOptionsExtension.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,65 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.EntityFrameworkCore.Infrastructure; | ||
using Microsoft.EntityFrameworkCore.Query; | ||
using Microsoft.EntityFrameworkCore.SqlServer.Properties; | ||
using Microsoft.EntityFrameworkCore.SqlServer.Internal; | ||
using Microsoft.EntityFrameworkCore.SqlServer.Query.ExpressionTranslators; | ||
using Microsoft.EntityFrameworkCore.SqlServer.Storage; | ||
using Microsoft.EntityFrameworkCore.Storage; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Microsoft.EntityFrameworkCore.SqlServer.Infrastructure | ||
namespace Microsoft.EntityFrameworkCore.SqlServer.Infrastructure; | ||
|
||
internal class SqlServerHierarchyIdOptionsExtension : IDbContextOptionsExtension | ||
{ | ||
internal class SqlServerHierarchyIdOptionsExtension : IDbContextOptionsExtension | ||
{ | ||
private DbContextOptionsExtensionInfo _info; | ||
private DbContextOptionsExtensionInfo? _info; | ||
|
||
public DbContextOptionsExtensionInfo Info => _info ??= new ExtensionInfo(this); | ||
public DbContextOptionsExtensionInfo Info => _info ??= new ExtensionInfo(this); | ||
|
||
public virtual void ApplyServices(IServiceCollection services) | ||
{ | ||
services.AddEntityFrameworkSqlServerHierarchyId(); | ||
} | ||
public virtual void ApplyServices(IServiceCollection services) | ||
{ | ||
services.AddEntityFrameworkSqlServerHierarchyId(); | ||
} | ||
|
||
public virtual void Validate(IDbContextOptions options) | ||
public virtual void Validate(IDbContextOptions options) | ||
{ | ||
var internalServiceProvider = options.FindExtension<CoreOptionsExtension>()?.InternalServiceProvider; | ||
if (internalServiceProvider != null) | ||
{ | ||
var internalServiceProvider = options.FindExtension<CoreOptionsExtension>()?.InternalServiceProvider; | ||
if (internalServiceProvider != null) | ||
using (var scope = internalServiceProvider.CreateScope()) | ||
{ | ||
using (var scope = internalServiceProvider.CreateScope()) | ||
if (scope.ServiceProvider.GetService<IEnumerable<IMethodCallTranslatorPlugin>>() | ||
?.Any(s => s is SqlServerHierarchyIdMethodCallTranslatorPlugin) != true || | ||
scope.ServiceProvider.GetService<IEnumerable<IRelationalTypeMappingSourcePlugin>>() | ||
?.Any(s => s is SqlServerHierarchyIdTypeMappingSourcePlugin) != true) | ||
{ | ||
if (scope.ServiceProvider.GetService<IEnumerable<IMethodCallTranslatorPlugin>>() | ||
?.Any(s => s is SqlServerHierarchyIdMethodCallTranslatorPlugin) != true || | ||
scope.ServiceProvider.GetService<IEnumerable<IRelationalTypeMappingSourcePlugin>>() | ||
?.Any(s => s is SqlServerHierarchyIdTypeMappingSourcePlugin) != true) | ||
{ | ||
throw new InvalidOperationException(Resources.ServicesMissing); | ||
} | ||
throw new InvalidOperationException(SqlServerHierarchyIdStrings.ServicesMissing); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private sealed class ExtensionInfo : DbContextOptionsExtensionInfo | ||
private sealed class ExtensionInfo : DbContextOptionsExtensionInfo | ||
{ | ||
public ExtensionInfo(IDbContextOptionsExtension extension) | ||
: base(extension) | ||
{ | ||
public ExtensionInfo(IDbContextOptionsExtension extension) | ||
: base(extension) | ||
{ | ||
} | ||
} | ||
|
||
private new SqlServerHierarchyIdOptionsExtension Extension | ||
=> (SqlServerHierarchyIdOptionsExtension)base.Extension; | ||
private new SqlServerHierarchyIdOptionsExtension Extension | ||
=> (SqlServerHierarchyIdOptionsExtension)base.Extension; | ||
|
||
public override bool IsDatabaseProvider => false; | ||
public override bool IsDatabaseProvider => false; | ||
|
||
public override int GetServiceProviderHashCode() => 0; | ||
public override int GetServiceProviderHashCode() => 0; | ||
|
||
public override bool ShouldUseSameServiceProvider(DbContextOptionsExtensionInfo other) | ||
=> other is ExtensionInfo; | ||
public override bool ShouldUseSameServiceProvider(DbContextOptionsExtensionInfo other) | ||
=> other is ExtensionInfo; | ||
|
||
public override void PopulateDebugInfo(IDictionary<string, string> debugInfo) | ||
=> debugInfo["SqlServer:" + nameof(SqlServerHierarchyIdDbContextOptionsBuilderExtensions.UseHierarchyId)] = "1"; | ||
public override void PopulateDebugInfo(IDictionary<string, string> debugInfo) | ||
=> debugInfo["SqlServer:" + nameof(SqlServerHierarchyIdDbContextOptionsBuilderExtensions.UseHierarchyId)] = "1"; | ||
|
||
public override string LogFragment => "using HierarchyId "; | ||
} | ||
public override string LogFragment => "using HierarchyId "; | ||
} | ||
} |
7 changes: 5 additions & 2 deletions
7
src/EFCore.SqlServer.HierarchyId/Properties/InternalsVisibleTo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
using System.Runtime.CompilerServices; | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Runtime.CompilerServices; | ||
|
||
[assembly: InternalsVisibleTo( | ||
"EntityFrameworkCore.SqlServer.HierarchyId.Test, PublicKey=00240000048000009400000006020000002400005253413100040000010001006d92138307b5e251bf4918cf751dc83489f4b2d70ac15b04110fd1f78491fe93719d0cd464d103a95fb1c3b1cb21ce0033c94c6f52b325d36360736dea7571bd1074cb2c937cf4fc54526ceb44271c4f44753dbeb5d9b364e4dc57a8988542d3a7edb6575bc35ce7670612bd8f00f2c6899f3e74bd563810fa45f4c5c8b51cd3")] | ||
"Microsoft.EntityFrameworkCore.SqlServer.HierarchyId.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")] |
Oops, something went wrong.