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.