niewolny.info … yet another .net blog.

28Nov/150

Accessing Entity Framework’s items with little help of metadata

Sometimes, when we work with database, we have to do some changes is some specific tables. It can be situation when we need correct some corrupted data in our system or compare some specific columns in tables between two databases. Instead of manual work with T-SQL scripts and spending effort to keep them up to date, we can write some program which will make our life easier on the one hand, and will save our time when it will come to maintenance. If we can use an ORM like Entity Framework then we can generate the model from the database, but here nice part ends. Now we will have few solutions available:

  • inspect our model with the help of "reflection" to discover the columns and properties which require our interference
  • modify T4 template, with which EF creates his objects, to inject some base classes or interfaces and to try to access the objects that way. Unfortunately not always we have opportunity to work with nicely structured database and not always making a change in T4 template is an easy task.
  • we can try to access metadata of Entity Framework and more easily access interesting objects for us to perform our modifications.

How we can access the metadata? Let's see below