Let’s say I have this DBSet
public DbSet<Category> Categories { get; set; }
and that it is used like this in my repository:
public virtual T GetById(long id)
{
T t = dbset.Find(id);
return t.Deleted ? null : t;
}
Let’s also say there is a Name property in Category, and that Category is a POCO object.
Is there a way to modify the content of Name between the database and the POCO object? Just for the sake of having an example, let’s say I want to capitalize all letters of Name in my Category POCO object. So, I could have Categories.Name = ‘Books’ in the database (Categories is a table in this case), and then have category.Name = “BOOKS” (where category is a POCO object) after issuing a command like:
Category category = categoryRepository.GetById(id);
Is there any way to achieve this? Is there a kind of a hook in DBSet where a function call can be inserted to transform data being transfered to a POCO object?
(Of course, I would then also need to be able to do the reverse operation when persisting a POCO objet to the database)
Why don’t you just add helper properties. For instance, using a partial class you could do this:
Then you can just do:
Or you could create an extension method on Category.