Created
June 12, 2021 23:32
-
-
Save allanfreitas/71918ea66f81957beda2bf8b31c234fd to your computer and use it in GitHub Desktop.
Criando um context de forma a passar para upper caso ou lowercase
This file contains hidden or 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
| namespace SeuNameSpace | |
| { | |
| public class ContextUpperLower : DbContext | |
| { | |
| public ContextUpperLower(DbContextOptions<eSistemLojaContexto> options) : base(options) | |
| { | |
| } | |
| protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | |
| { | |
| #if DEBUG //Só funciona .net 5 | |
| optionsBuilder.EnableSensitiveDataLogging(); | |
| optionsBuilder.EnableDetailedErrors(); | |
| optionsBuilder.EnableSensitiveDataLogging(); // Define para ser apresentado logs detalhados | |
| optionsBuilder.LogTo(Console.WriteLine, | |
| new[] {RelationalEventId.CommandExecuted}); | |
| #endif | |
| } | |
| protected override void OnModelCreating(ModelBuilder modelBuilder) | |
| { | |
| //Aqui vc usa o lowercase ou uppercase, poderia até mesmo ser o mesmo método só definindo se é upper ou lower ok. | |
| //ToLowerObjectNames(modelBuilder); | |
| ToUpperContext(modelBuilder); | |
| modelBuilder.ApplyConfigurationsFromAssembly(typeof(ContextUpperLower).Assembly); | |
| } | |
| /// <summary> | |
| /// Transforma todos os names das propriedades por padrão em lowercase | |
| /// </summary> | |
| /// <param name="modelBuilder"></param> | |
| private static void ToLowerObjectNames(ModelBuilder modelBuilder) | |
| { | |
| foreach (IMutableEntityType entity in modelBuilder.Model.GetEntityTypes()) | |
| { | |
| if (typeof(IBaseIdentity).IsAssignableFrom(entity.ClrType)) | |
| { | |
| string tablename = entity.GetTableName().ToLowerContext(); | |
| entity.SetTableName(tablename); | |
| foreach (IMutableProperty property in entity.GetProperties()) | |
| { | |
| string columnName = property.GetColumnName().ToLowerContext(); | |
| property.SetColumnName(columnName); | |
| } | |
| foreach (IMutableKey key in entity.GetKeys()) | |
| { | |
| string keyName = key.GetName().ToLowerContext(); | |
| key.SetName(keyName); | |
| } | |
| foreach (IMutableForeignKey key in entity.GetForeignKeys()) | |
| { | |
| string keyName = key.GetConstraintName().ToLowerContext(); | |
| key.SetConstraintName(keyName); | |
| } | |
| foreach (IMutableIndex index in entity.GetIndexes()) | |
| { | |
| if (index == null) continue; | |
| string keyName = index.GetName().ToLowerContext(); | |
| index.SetName(keyName); | |
| } | |
| } | |
| } | |
| } | |
| /// <summary> | |
| /// Transforma todos os names das propriedades por padrão em UpperCase | |
| /// Caso queira modificar apenas o nomes dos campos/colunas, fazer o processamento apenas do primeiro foreach que trata as colunas ok. | |
| /// </summary> | |
| /// <param name="modelBuilder"></param> | |
| private static void ToUpperObjectNames(ModelBuilder modelBuilder) | |
| { | |
| foreach (IMutableEntityType entity in modelBuilder.Model.GetEntityTypes()) | |
| { | |
| if (typeof(IBaseIdentity).IsAssignableFrom(entity.ClrType)) | |
| { | |
| //Nome das Tabelas | |
| string tablename = entity.GetTableName().ToUpperContext(); | |
| entity.SetTableName(tablename); | |
| foreach (IMutableProperty property in entity.GetProperties()) | |
| { | |
| //Nome das Colunas | |
| string columnName = property.GetColumnName().ToUpperContext(); | |
| property.SetColumnName(columnName); | |
| } | |
| foreach (IMutableKey key in entity.GetKeys()) | |
| { | |
| //Nome dos indices | |
| string keyName = key.GetName().ToUpperContext(); | |
| key.SetName(keyName); | |
| } | |
| foreach (IMutableForeignKey key in entity.GetForeignKeys()) | |
| { | |
| //Nome das ForeingKey | |
| string keyName = key.GetConstraintName().ToUpperContext(); | |
| key.SetConstraintName(keyName); | |
| } | |
| foreach (IMutableIndex index in entity.GetIndexes()) | |
| { | |
| if (index == null) continue; | |
| //Nome das colunas keys | |
| string keyName = index.GetName().ToUpperContext(); | |
| index.SetName(keyName); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| public static class Utils | |
| { | |
| /// <summary> | |
| /// Processa string para o estilo UpperCase, todas os carcteres minúsculos. Exemplo: ItemNotaFiscal = ITENNOTAFISCAL | |
| /// </summary> | |
| /// <param name="data">string a ser processada</param> | |
| /// <returns>String em upperCase (letras Maiúsculas)</returns> | |
| public static string ToUpperContext(this string data) => | |
| (string.IsNullOrWhiteSpace(data) | |
| ? data | |
| : Regex.Replace(data, @"([a-z0-9])([A-Z])", "$1$2").ToUpper()); | |
| /// <summary> | |
| /// Processa string para o estilo LowerCase, todas os carcteres minúsculos. Exemplo: ItemNotaFiscal = itennotafiscal | |
| /// </summary> | |
| /// <param name="data">string a ser processada</param> | |
| /// <returns>String em lowercase (letras Minúsculas)</returns> | |
| public static string ToLowerContext(this string data) => | |
| (string.IsNullOrWhiteSpace(data) | |
| ? data | |
| : Regex.Replace(data, @"([a-z0-9])([A-Z])", "$1$2").ToLower()); | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment