EF 4.1 Code First Disable checking for EdmMetadata table

When you use code first, EF will generate the database for you based on your class. Subsequently when you make changes to the classes EF is smart enough to detect the changes and warn you to regenerate the database or it regenerates the database for you based on your setting. You can achieve this by using Database.Setinitializer in the system.data.entity namespace.


You have two options
1.    Drop Create Database Always or
2.    Drop Create Database If Model Changes

EF creates a table EdmMetadata in your database, creates a hash of your entities and then every time you run the application it compares the hash to see if there were changes to the model. Behind the scene you will see the below sql being run first.

SELECT   TOP ( 1 ) [Extent1].[Id]        AS [Id],
[Extent1].[ModelHash] AS [ModelHash]
FROM     [dbo].[EdmMetadata] AS [Extent1]
ORDER BY [Extent1].[Id] DESC

This works well for your initial development, but after you have the core database ready and development as started you could generally want to avoid this, or if your scenario is Code first with existing database then you would avoid this.

Let us assume you started with an existing database and code first, or you started with code first and then deleted EdmMetadata as it serves no purpose. When you run the application an exception will occur behind the scene which will not be obvious in your application, because EF does not care if EdmMetadata is there are not. If it is not there it assumes you are tracking the changes and you are in-charge, so it ignores the exceptions. You can see this by using EF Profiler



The way to avoid this exception and let EF know we are in-charge of the Metadata is to add the below line in the your OnModelCreating Method

Protected Overrides Sub OnModelCreating(ByVal modelBuilder As DbModelBuilder)

‘To disable checking for EdmMetadata table.
modelBuilder.Conventions.Remove(Of IncludeMetadataConvention)()

One thought on “EF 4.1 Code First Disable checking for EdmMetadata table

  1. Great! However, it does still seem to do more (unnessecary?) data access. At least it opens a dedicated connection for that…

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s