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>pull/23250/head
parent
c0cfbacdef
commit
0dc67fea32
|
@ -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 <xref:Microsoft.Extensions.Configuration.IConfigurationSource>.
|
||||
|
||||
*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 <xref:Microsoft.Extensions.Configuration.ConfigurationProvider>. 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)]
|
||||
|
||||
<a name="acs"></a>
|
||||
|
||||
## 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
|
||||
|
||||
|
|
|
@ -10,6 +10,6 @@ public class EFConfigurationContext : DbContext
|
|||
{
|
||||
}
|
||||
|
||||
public DbSet<EFConfigurationValue>? Values => Set<EFConfigurationValue>();
|
||||
public DbSet<EFConfigurationValue> Values => Set<EFConfigurationValue>();
|
||||
}
|
||||
#endregion
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace EFConfigSample;
|
|
@ -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();
|
Loading…
Reference in New Issue