From fd758d4ec6072475dc7aedb24c45e5fd05ae404a Mon Sep 17 00:00:00 2001 From: Pawel Gerr Date: Sun, 7 Feb 2021 20:01:53 +0100 Subject: [PATCH] AddEnumAndValueTypeConverters can take a delegate for further configuration of the EF property. --- .../ProductsDbContext.cs | 18 +++++++++++-- .../Extensions/ModelBuilderExtensions.cs | 26 ++++++++++++++----- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/samples/Thinktecture.Runtime.Extensions.EntityFrameworkCore.Samples/ProductsDbContext.cs b/samples/Thinktecture.Runtime.Extensions.EntityFrameworkCore.Samples/ProductsDbContext.cs index cab12cfd..05926f55 100644 --- a/samples/Thinktecture.Runtime.Extensions.EntityFrameworkCore.Samples/ProductsDbContext.cs +++ b/samples/Thinktecture.Runtime.Extensions.EntityFrameworkCore.Samples/ProductsDbContext.cs @@ -1,4 +1,6 @@ -using Microsoft.EntityFrameworkCore; +using System.Linq; +using Microsoft.EntityFrameworkCore; +using Thinktecture.EnumLikeClasses; namespace Thinktecture { @@ -27,7 +29,19 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) }); }); - modelBuilder.AddEnumAndValueTypeConverters(true); + modelBuilder.AddEnumAndValueTypeConverters(true, property => + { + if (property.ClrType == typeof(SpecialProductType)) + { + var maxLength = SpecialProductType.Items.Max(i => i.Key.Length); + property.SetMaxLength(RoundUp(maxLength)); + } + }); + } + + private static int RoundUp(int value) + { + return value + (10 - value % 10); } } } diff --git a/src/Thinktecture.Runtime.Extensions.EntityFrameworkCore/Extensions/ModelBuilderExtensions.cs b/src/Thinktecture.Runtime.Extensions.EntityFrameworkCore/Extensions/ModelBuilderExtensions.cs index cb300baa..42736a67 100644 --- a/src/Thinktecture.Runtime.Extensions.EntityFrameworkCore/Extensions/ModelBuilderExtensions.cs +++ b/src/Thinktecture.Runtime.Extensions.EntityFrameworkCore/Extensions/ModelBuilderExtensions.cs @@ -20,22 +20,30 @@ public static class ModelBuilderExtensions /// /// EF model builder. /// In case of an , ensures that the item is valid before writing it to database. + /// Action for further configuration of the property. /// If is null. public static void AddEnumAndValueTypeConverters( this ModelBuilder modelBuilder, - bool validateOnWrite) + bool validateOnWrite, + Action? configure = null) { if (modelBuilder is null) throw new ArgumentNullException(nameof(modelBuilder)); + configure ??= Empty.Action; + foreach (var entity in modelBuilder.Model.GetEntityTypes()) { - AddConverterForScalarProperties(entity, validateOnWrite); - AddConvertersForNavigations(entity, modelBuilder, validateOnWrite); + AddConverterForScalarProperties(entity, validateOnWrite, configure); + AddConvertersForNavigations(entity, modelBuilder, validateOnWrite, configure); } } - private static void AddConvertersForNavigations(IMutableEntityType entity, ModelBuilder modelBuilder, bool validateOnWrite) + private static void AddConvertersForNavigations( + IMutableEntityType entity, + ModelBuilder modelBuilder, + bool validateOnWrite, + Action configure) { List? navigationsToConvert = null; @@ -50,12 +58,17 @@ private static void AddConvertersForNavigations(IMutableEntityType entity, Model foreach (var navigation in navigationsToConvert) { var valueConverter = ValueTypeValueConverterFactory.Create(navigation.ClrType, validateOnWrite); - modelBuilder.Entity(entity.ClrType).Property(navigation.Name).HasConversion(valueConverter); + var property = modelBuilder.Entity(entity.ClrType).Property(navigation.Name); + property.HasConversion(valueConverter); + configure(property.Metadata); } } } - private static void AddConverterForScalarProperties(IMutableEntityType entity, bool validateOnWrite) + private static void AddConverterForScalarProperties( + IMutableEntityType entity, + bool validateOnWrite, + Action configure) { foreach (var property in entity.GetProperties()) { @@ -69,6 +82,7 @@ private static void AddConverterForScalarProperties(IMutableEntityType entity, b valueConverter = ValueTypeValueConverterFactory.Create(property.ClrType, validateOnWrite); property.SetValueConverter(valueConverter); + configure(property); } } }