Skip to content

Instantly share code, notes, and snippets.

@emrekizildas
Created February 20, 2022 10:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emrekizildas/54976d28776b640ac838992785a1deb1 to your computer and use it in GitHub Desktop.
Save emrekizildas/54976d28776b640ac838992785a1deb1 to your computer and use it in GitHub Desktop.
using ConverterExample.Converter;
using ConverterExample.Attribute;
using System.Linq;
using System.Reflection;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using System;
namespace ConverterExample.Extension
{
public static class ModelBuilderExtension
{
public static void UseAlwaysUpper(this ModelBuilder modelBuilder)
{
var upperConverter = new UpperConverter();
foreach (IMutableEntityType entityType in modelBuilder.Model.GetEntityTypes())
{
foreach (IMutableProperty property in entityType.GetProperties())
{
if(property.ClrType == typeof(string))
{
object[] attributes = property.PropertyInfo.GetCustomAttributes(typeof(AlwaysUpperAttribute), false);
if(attributes.Any())
property.SetValueConverter(upperConverter);
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment