Entity code first with shared DB

So I have an MVC4 project running on localhost and it's time to upload it to a shared environment.

Being that I am using a co-hosted framework, I cannot use inline initializers. So I backed up my mdf (.bak) file and uploaded it to my production environment. Everything looked right ... The tables were there ... but when I run my application I get this exception:

The model that supports the "YourContext" context has changed since the database was created. Consider using First First Migrations to update your database ( http://go.microsoft.com/fwlink/?LinkId=238269 ).


It worked!

I added a custom initializer.

public class ProductionInitializer : IDatabaseInitializer<AvariceContext>
{
    public void InitializeDatabase(AvariceContext context)
    {
    }
}

      

The solution is ... hackish, but it works. If anyone could shed some light I would appreciate it.

+3


source to share


2 answers


If you are sure you have not updated the EF model since the database was created, you can simply drop the _MigrationHistory table. This is just used to keep the db and model in sync, but it doesn't work in the script you have highlighted.

In the future, you can still use the Migrations EF feature by adding a '- script' to the flag to the update-database command that you run in the development environment as shown below. This will result in a migration script that you can run on a production database.



> update-database -script

      

0


source


"Got this action! I added a custom initializer."

Why it works: ... You can access the database even though EF sees some subtle difference. Your code implements IDatabaseInitializer and ignores the option (empty method) to initialize or MIGRATE to the latest version. So Ef thinks DB is wrong and you continue anyway.

Lack of a long-term approach to EF's access to steady state. I would take a look at how to work with Migrate to the latest version.



Being that I am using a co-hosted framework, I cannot use inline initializers. "

Why not?

0


source







All Articles