Skip to content

Commit

Permalink
Add some bulk configuration docs in the value converters page
Browse files Browse the repository at this point in the history
  • Loading branch information
roji committed Feb 3, 2022
1 parent aaa347e commit 02181a6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
4 changes: 2 additions & 2 deletions entity-framework/core/modeling/bulk-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ Properties of this type are not discovered by default as the current EF provider

## Pre-convention configuration

EF Core 6.0 allows the mapping configuration to be specified once for a given CLR type; that configuration is then applied to all properties of that type in the model as they are discovered. This is called "pre-convention model configuration", since it configures aspects of the model that are then used by the model building conventions. Such configuration is applied by overriding `<xref:Microsoft.EntityFrameworkCore.DbContext.ConfigureConventions>` on the type derived from `<xref:Microsoft.EntityFrameworkCore.DbContext>`.
EF Core 6.0 allows the mapping configuration to be specified once for a given CLR type; that configuration is then applied to all properties of that type in the model as they are discovered. This is called "pre-convention model configuration", since it configures aspects of the model that are then used by the model building conventions. Such configuration is applied by overriding <xref:Microsoft.EntityFrameworkCore.DbContext.ConfigureConventions%2A> on the type derived from <xref:Microsoft.EntityFrameworkCore.DbContext>.

This example shows how configure all properties of type `Currency` to have a value converter:

[!code-csharp[Main](../../../samples/core/Modeling/BulkConfiguration/PreConventionContext.cs?name=CurrencyConversion)]
[!code-csharp[Main](../../../samples/core/Modeling/BulkConfiguration/CurrencyContext.cs?name=ConfigureConventions)]

And this example shows how to configure some facets on all properties of type `string`:

Expand Down
10 changes: 10 additions & 0 deletions entity-framework/core/modeling/value-conversions.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ Conversions can be configured in <xref:Microsoft.EntityFrameworkCore.DbContext.O
> [!NOTE]
> A `null` value will never be passed to a value converter. A null in a database column is always a null in the entity instance, and vice-versa. This makes the implementation of conversions easier and allows them to be shared amongst nullable and non-nullable properties. See [GitHub issue #13850](https://github.com/dotnet/efcore/issues/13850) for more information.
### Bulk-configuring a value converter

It's common for the same value converter to be configured for every property that uses the relevant CLR type. Rather than doing this manually for each property, you can use [pre-convention model configuration](xref:core/modeling/bulk-configuration#pre-convention-configuration) to do this once for your entire model. To do this, define your value converter as a class:

[!code-csharp[Main](../../../samples/core/Modeling/BulkConfiguration/CurrencyConverter.cs?name=CurrencyConverter)]

Then, override <xref:Microsoft.EntityFrameworkCore.DbContext.ConfigureConventions%2A> in your context type and configure the converter as follows:

[!code-csharp[Main](../../../samples/core/Modeling/BulkConfiguration/CurrencyContext.cs?name=ConfigureConventions)]

## Pre-defined conversions

EF Core contains many pre-defined conversions that avoid the need to write conversion functions manually. Instead, EF Core will pick the conversion to use based on the property type in the model and the requested database provider type.
Expand Down
26 changes: 26 additions & 0 deletions samples/core/Modeling/BulkConfiguration/CurrencyContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;

namespace EFModeling.BulkConfiguration
{
public class CurrencyContext : DbContext
{
public DbSet<Product> Products { get; set; }

#region ConfigureConventions
protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
{
configurationBuilder
.Properties<Currency>()
.HaveConversion<CurrencyConverter>();
}
#endregion

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseSqlServer(
@"Server=(localdb)\mssqllocaldb;Database=EFModeling.BulkConfiguration.Currency;Trusted_Connection=True")
.LogTo(Console.WriteLine, minimumLevel: Microsoft.Extensions.Logging.LogLevel.Information)
.EnableSensitiveDataLogging();
}
}

0 comments on commit 02181a6

Please sign in to comment.