Created
February 20, 2022 10:54
-
-
Save emrekizildas/54976d28776b640ac838992785a1deb1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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