Skip to content

Commit

Permalink
AddEnumAndValueTypeConverters can take a delegate for further configu…
Browse files Browse the repository at this point in the history
…ration of the EF property.
  • Loading branch information
PawelGerr committed Feb 7, 2021
1 parent cae4040 commit fd758d4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Microsoft.EntityFrameworkCore;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Thinktecture.EnumLikeClasses;

namespace Thinktecture
{
Expand Down Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,30 @@ public static class ModelBuilderExtensions
/// </summary>
/// <param name="modelBuilder">EF model builder.</param>
/// <param name="validateOnWrite">In case of an <see cref="IValidatableEnum{TKey}"/>, ensures that the item is valid before writing it to database.</param>
/// <param name="configure">Action for further configuration of the property.</param>
/// <exception cref="ArgumentNullException">If <paramref name="modelBuilder"/> is <c>null</c>.</exception>
public static void AddEnumAndValueTypeConverters(
this ModelBuilder modelBuilder,
bool validateOnWrite)
bool validateOnWrite,
Action<IMutableProperty>? 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<IMutableProperty> configure)
{
List<IMutableNavigation>? navigationsToConvert = null;

Expand All @@ -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<IMutableProperty> configure)
{
foreach (var property in entity.GetProperties())
{
Expand All @@ -69,6 +82,7 @@ private static void AddConverterForScalarProperties(IMutableEntityType entity, b

valueConverter = ValueTypeValueConverterFactory.Create(property.ClrType, validateOnWrite);
property.SetValueConverter(valueConverter);
configure(property);
}
}
}
Expand Down

0 comments on commit fd758d4

Please sign in to comment.