Entity Framework – Get the mapping between Table, Entity, Properties and Columns at runtime (EF 6.1)

Recently, I wanted to dump the content of a DbContext. Therefor, I needed to know some information on how Entity and properties were mapping to Tables and columns. (I wanted the output to contain table and column names from my DB schema)
If you are in the same situation, just check the following project : CExtensions-Net.

Get the package from nuget :(from version 1.1.0 in pre at the time of writing)

  Install-Package CExtensions.EntityFramework -Pre

With this in place, getting the mapping for an entity is as simple as:


  var entityMapping = someDbContext.GetMappings<Author>();
 
  entityMapping.Table.ShouldBe("AUTHOR");
  entityMapping.MappedColumn("FirstName").ShouldBe("AUT_FIRSTNAME");

If you want to know the mapping for some Table, just do this:


  entityMapping = someDbContext.GetMappings("AUTHOR");
  
  entityMapping.Entity.ShouldBe("Author");
  entityMapping.ClrType.ShouldBe(typeof(Author));
                

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s