From 0dc67fea324098f3ba6ab0cac3bb17fc3b664208 Mon Sep 17 00:00:00 2001 From: Fiyaz Bin Hasan Date: Thu, 9 Sep 2021 20:02:18 +0600 Subject: [PATCH] section added for custom configuration provider for .net 6 (#23247) * section added for custom configuration provider for .net 6 * add sample snippet to EfconfigSample folder * EFConfigurationSource.cs * Delete EFConfigurationSource .cs Co-authored-by: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> --- .../fundamentals/configuration/index.md | 25 ++++++------------- .../EFConfigurationContext.cs | 2 +- ...ionSource .cs => EFConfigurationSource.cs} | 1 - .../6.x/EfconfigSample/Program.cs | 10 ++++++++ 4 files changed, 18 insertions(+), 20 deletions(-) rename aspnetcore/fundamentals/configuration/index/samples/6.x/EFconfigSample/EFConfigurationProvider/{EFConfigurationSource .cs => EFConfigurationSource.cs} (99%) create mode 100644 aspnetcore/fundamentals/configuration/index/samples_snippets/6.x/EfconfigSample/Program.cs diff --git a/aspnetcore/fundamentals/configuration/index.md b/aspnetcore/fundamentals/configuration/index.md index 97d5c0eba9..4437d6c15d 100644 --- a/aspnetcore/fundamentals/configuration/index.md +++ b/aspnetcore/fundamentals/configuration/index.md @@ -726,6 +726,7 @@ Index: 5 Value: value5 ``` Custom configuration providers aren't required to implement array binding. +--> ## Custom configuration provider @@ -741,47 +742,35 @@ Define an `EFConfigurationValue` entity for storing configuration values in the *Models/EFConfigurationValue.cs*: -[!code-csharp[](index/samples/3.x/ConfigurationSample/Models/EFConfigurationValue.cs?name=snippet1)] +[!code-csharp[](index/samples/6.x/EfconfigSample/Models/EFConfigurationValue.cs?name=snippet1)] Add an `EFConfigurationContext` to store and access the configured values. *EFConfigurationProvider/EFConfigurationContext.cs*: -[!code-csharp[](index/samples/3.x/ConfigurationSample/EFConfigurationProvider/EFConfigurationContext.cs?name=snippet1)] +[!code-csharp[](index/samples/6.x/EfconfigSample/EFConfigurationProvider/EFConfigurationContext.cs?name=snippet1)] Create a class that implements . *EFConfigurationProvider/EFConfigurationSource.cs*: -[!code-csharp[](index/samples/3.x/ConfigurationSample/EFConfigurationProvider/EFConfigurationSource.cs?name=snippet1)] +[!code-csharp[](index/samples/6.x/EfconfigSample/EFConfigurationProvider/EFConfigurationSource.cs?name=snippet1)] Create the custom configuration provider by inheriting from . The configuration provider initializes the database when it's empty. Since configuration keys are case-insensitive, the dictionary used to initialize the database is created with the case-insensitive comparer ([StringComparer.OrdinalIgnoreCase](xref:System.StringComparer.OrdinalIgnoreCase)). *EFConfigurationProvider/EFConfigurationProvider.cs*: -[!code-csharp[](index/samples/3.x/ConfigurationSample/EFConfigurationProvider/EFConfigurationProvider.cs?name=snippet1)] +[!code-csharp[](index/samples/6.x/EfconfigSample/EFConfigurationProvider/EFConfigurationProvider.cs?name=snippet1)] An `AddEFConfiguration` extension method permits adding the configuration source to a `ConfigurationBuilder`. *Extensions/EntityFrameworkExtensions.cs*: -[!code-csharp[](index/samples/3.x/ConfigurationSample/Extensions/EntityFrameworkExtensions.cs?name=snippet1)] +[!code-csharp[](index/samples/6.x/EfconfigSample/Extensions/EntityFrameworkExtensions.cs?name=snippet1)] The following code shows how to use the custom `EFConfigurationProvider` in *Program.cs*: -[!code-csharp[](index/samples_snippets/3.x/ConfigurationSample/Program.cs?highlight=7-8)] - - - -## Access configuration in Startup - -The following code displays configuration data in `Startup` methods: - -[!code-csharp[](index/samples/3.x/ConfigSample/StartupKey.cs?name=snippet&highlight=13,18)] - -For an example of accessing configuration using startup convenience methods, see [App startup: Convenience methods](xref:fundamentals/startup#convenience-methods). - ---> +[!code-csharp[](index/samples_snippets/6.x/EfconfigSample/Program.cs?highlight=5-6)] ## Access configuration in Razor Pages diff --git a/aspnetcore/fundamentals/configuration/index/samples/6.x/EFconfigSample/EFConfigurationProvider/EFConfigurationContext.cs b/aspnetcore/fundamentals/configuration/index/samples/6.x/EFconfigSample/EFConfigurationProvider/EFConfigurationContext.cs index ca9f419752..fed34f59b9 100644 --- a/aspnetcore/fundamentals/configuration/index/samples/6.x/EFconfigSample/EFConfigurationProvider/EFConfigurationContext.cs +++ b/aspnetcore/fundamentals/configuration/index/samples/6.x/EFconfigSample/EFConfigurationProvider/EFConfigurationContext.cs @@ -10,6 +10,6 @@ public class EFConfigurationContext : DbContext { } - public DbSet? Values => Set(); + public DbSet Values => Set(); } #endregion diff --git a/aspnetcore/fundamentals/configuration/index/samples/6.x/EFconfigSample/EFConfigurationProvider/EFConfigurationSource .cs b/aspnetcore/fundamentals/configuration/index/samples/6.x/EFconfigSample/EFConfigurationProvider/EFConfigurationSource.cs similarity index 99% rename from aspnetcore/fundamentals/configuration/index/samples/6.x/EFconfigSample/EFConfigurationProvider/EFConfigurationSource .cs rename to aspnetcore/fundamentals/configuration/index/samples/6.x/EFconfigSample/EFConfigurationProvider/EFConfigurationSource.cs index 6e3d85006b..658a566055 100644 --- a/aspnetcore/fundamentals/configuration/index/samples/6.x/EFconfigSample/EFConfigurationProvider/EFConfigurationSource .cs +++ b/aspnetcore/fundamentals/configuration/index/samples/6.x/EFconfigSample/EFConfigurationProvider/EFConfigurationSource.cs @@ -1,4 +1,3 @@ - using Microsoft.EntityFrameworkCore; namespace EFConfigSample; diff --git a/aspnetcore/fundamentals/configuration/index/samples_snippets/6.x/EfconfigSample/Program.cs b/aspnetcore/fundamentals/configuration/index/samples_snippets/6.x/EfconfigSample/Program.cs new file mode 100644 index 0000000000..a5bfc27f38 --- /dev/null +++ b/aspnetcore/fundamentals/configuration/index/samples_snippets/6.x/EfconfigSample/Program.cs @@ -0,0 +1,10 @@ +//using Microsoft.EntityFrameworkCore; + +var builder = WebApplication.CreateBuilder(args); + +builder.Configuration.AddEFConfiguration( + opt => opt.UseInMemoryDatabase("InMemoryDb")); + +var app = builder.Build(); + +app.Run();