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

To access the metadata we need first get access to the ObjectContext of Entity Framework by casting our DbContext to IObjectContextAdapter interface. Once we have an access to the ObjectContext we can get through to MetadataWorkspace which allows us to access different metadata information.

To get a metadata we can call GetItems method and define what kind of information we would like to get. There are 5 spaces available (description of each space comes from here):

  • C-Space – This is where the metadata about our conceptual model is found. Here we will get access to all Edm objects and the tables in our generated model.
  • S-Space – This is where metadata about the database is found. Here we will get access to all Sql objects and the tables in our database
  • O-Space – This is where metadata about the CLR types that map to our conceptual model is found
  • CS-Space – This is where metadata about mapping is found
  • OC-Space – This is where EF holds the mapping between our conceptual model (C-Space) and the CLR objects (O-Space).

With GetItems method we get all the objects which we have to filter out ourselves to get the information we need. However we can also ask for the EntityContainer from the conceptual model to access the models generated in our C# code.

With the container we get access to collection of AssociationSets, EntitySets and BaseEntitySets (which delivers list of entities and associations in given container). Now we can easily get either all tables which are associated with given table or we can search for all entities which have a specified property.

We can also go further and search for example for all identity columns in given EntitySet:

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.