Entity Framework Core 2 Save strings as uppercase

.net-core-2.0 entity-framework entity-framework-core

Question

I want to save all strings to my DB in uppercase. I think the best place to do this is by overriding SaveChanges() on my DbContext. I know I need to call ToUpper() on something but I am unsure on what to call it on.

public override int SaveChanges()
{
    foreach (var entry in ChangeTracker.Entries().Where(e => e.State == EntityState.Added || e.State == EntityState.Modified))
    {
       //do something
    }
    return base.SaveChanges();
}
1
0
8/18/2017 4:04:22 AM

Popular Answer

I'm not sure if it is wise to pollute your Database layer with this constraint. This would limit reusability of your database.

Quite often the definition of structure of the (tables of the) database is separated from the handling of the data in the database, which is done via a separate Database Abstraction Layer.

This separation makes it possible to reuse your database structure for databases with other constraints (for instance, one that allows lower case strings, or a special database for unit tests).

This seperation of concerns is quite often implemented using the repository pattern. This separation makes it possible to change functionality of the database without having to change the structure of the database.

You could also use an existing database that uses lower case strings, as your repository layer could convert everything to upper case before returning queried strings.

So by separating the database from functionality on the data you make it easier to reuse the database for other purposes, and to change requirements without having to change the data in the database: improved reusability and improved maintenance.

0
8/18/2017 7:53:31 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow