From aaa347ec62dc7156f817849b4982a9c40c7f3b35 Mon Sep 17 00:00:00 2001 From: Shay Rojansky Date: Thu, 3 Feb 2022 10:04:03 +0100 Subject: [PATCH 1/3] Document that value-converted types aren't support in raw SQL Also remove the limitation that value converters can't be bulk-configured, that was done in 6.0. Relates to https://github.com/dotnet/efcore/issues/27354 --- entity-framework/core/modeling/value-conversions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/entity-framework/core/modeling/value-conversions.md b/entity-framework/core/modeling/value-conversions.md index e45d70270d..a685be565e 100644 --- a/entity-framework/core/modeling/value-conversions.md +++ b/entity-framework/core/modeling/value-conversions.md @@ -781,10 +781,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 #11597](https://github.com/dotnet/efcore/issues/27354) if this is something you need. Removal of these limitations is being considered for future releases. From 02181a6db84737cb182f9de57c7ac37b10bef103 Mon Sep 17 00:00:00 2001 From: Shay Rojansky Date: Thu, 3 Feb 2022 10:34:31 +0100 Subject: [PATCH 2/3] Add some bulk configuration docs in the value converters page --- .../core/modeling/bulk-configuration.md | 4 +-- .../core/modeling/value-conversions.md | 10 +++++++ .../BulkConfiguration/CurrencyContext.cs | 26 +++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 samples/core/Modeling/BulkConfiguration/CurrencyContext.cs 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 a685be565e..afb20e4471 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. 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(); + } +} From 051ae378f1ccaefaec9eedeffaec0ac9024eac9c Mon Sep 17 00:00:00 2001 From: Shay Rojansky Date: Thu, 3 Feb 2022 19:40:44 +0200 Subject: [PATCH 3/3] Update entity-framework/core/modeling/value-conversions.md Co-authored-by: Smit Patel --- entity-framework/core/modeling/value-conversions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/entity-framework/core/modeling/value-conversions.md b/entity-framework/core/modeling/value-conversions.md index afb20e4471..17c4efc00a 100644 --- a/entity-framework/core/modeling/value-conversions.md +++ b/entity-framework/core/modeling/value-conversions.md @@ -795,6 +795,6 @@ There are a few known current limitations of the value conversion system: * 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 #11597](https://github.com/dotnet/efcore/issues/27354) 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.