Wednesday, April 27, 2016

System.InvalidOperationException: Migrations is enabled for context but the database does not exist or contains no mapped tables.

Problem:
Entity Framework throws migration exception when trying to move code to Staging Server.


I have been trying out the EF migration and Code First approach on a new project.
I was able to get the migration and update working on my dev box.  I then went to move
the code base to our staging using xcopy.   I then hit the website and got a nasty no mapped
tables error.


Solution:


For me I don't need to move the migrations to the staging or production server.  I just needed to disable the migration.  This can be done using the web.config:




This is the basic syntax for setting an initializer in your config file:
<appSettings>
< add key=”DatabaseInitializerForType MyAssemblyQualifiedContextType”
value=”MyAssemblyQualifedInitializerType” />
< /appSettings>

The key things to notice here are:
  • The entry goes in the appSettings section of your config file
  • The key must always start with the string “DatabaseInitializerForType” followed by whitespace
  • The second part of the key is the assembly-qualified name of the context type for which the initializer should be set
  • The value is the assembly-qualified name of the initializer to set, which must expose a public, parameterless constructor so that EF can create an instance of it
Example:


<add key="DatabaseInitializerForType FCJC.Models.FCJCContext"
value="Disabled"/>


I placed this in my web.staging.config so that each time I build out my solution for deployment to staging, it will insert this flag and no more error!






Source:
https://blog.oneunicorn.com/2011/03/31/configuring-database-initializers-in-a-config-file/

No comments:

Post a Comment