diff --git a/entity-framework/core/modeling/bulk-configuration.md b/entity-framework/core/modeling/bulk-configuration.md index 4eb9898739..6d654f2ba4 100644 --- a/entity-framework/core/modeling/bulk-configuration.md +++ b/entity-framework/core/modeling/bulk-configuration.md @@ -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 `` on the type derived from ``. +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 on the type derived from . 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`: diff --git a/entity-framework/core/modeling/value-conversions.md b/entity-framework/core/modeling/value-conversions.md index e45d70270d..17c4efc00a 100644 --- a/entity-framework/core/modeling/value-conversions.md +++ b/entity-framework/core/modeling/value-conversions.md @@ -60,6 +60,16 @@ Conversions can be configured in [!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 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. @@ -781,10 +791,10 @@ Value converters can be used to encrypt property values before sending them to t There are a few known current limitations of the value conversion system: -* There is currently no way to specify in one place that every property of a given type must use the same value converter. Please vote (👍) for [GitHub issue #10784](https://github.com/dotnet/efcore/issues/10784) if this is something you need. * As noted above, `null` cannot be converted. Please vote (👍) for [GitHub issue #13850](https://github.com/dotnet/efcore/issues/13850) if this is something you need. * There is currently no way to spread a conversion of one property to multiple columns or vice-versa. Please vote (👍) for [GitHub issue #13947](https://github.com/dotnet/efcore/issues/13947) if this is something you need. * Value generation is not supported for most keys mapped through value converters. Please vote (👍) for [GitHub issue #11597](https://github.com/dotnet/efcore/issues/11597) if this is something you need. * Value conversions cannot reference the current DbContext instance. Please vote (👍) for [GitHub issue #11597](https://github.com/dotnet/efcore/issues/12205) if this is something you need. +* Parameters using value-converted types cannot currently be used in raw SQL APIs. Please vote (👍) for [GitHub issue #27534](https://github.com/dotnet/efcore/issues/27354) if this is something you need. Removal of these limitations is being considered for future releases. diff --git a/samples/core/Modeling/BulkConfiguration/CurrencyContext.cs b/samples/core/Modeling/BulkConfiguration/CurrencyContext.cs new file mode 100644 index 0000000000..7a1f1adcfb --- /dev/null +++ b/samples/core/Modeling/BulkConfiguration/CurrencyContext.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using Microsoft.EntityFrameworkCore; + +namespace EFModeling.BulkConfiguration +{ + public class CurrencyContext : DbContext + { + public DbSet Products { get; set; } + + #region ConfigureConventions + protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder) + { + configurationBuilder + .Properties() + .HaveConversion(); + } + #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(); + } +}