Database is not getting created at first time database-migration entity-framework-6


How to re-create the database using EF6?

I have tried both of following post but, i don't know why its not working and getting same error.

  1. How do I generate an EF6 database with migrations enabled, without using update-database?
  2. Migrations is enabled for context ''but the database does not exist or contains no mapped tables

I have already published my sample on the web server. I am using Sql Server 2012 Express DBMS.

When i have created my application and published it on web server it was working fine. It has created database automatically. After that i have made some changes in ApplicationDBContext and added some properties in IdentityModes(ApplicationUser). Then I have migrated database on my Local IIS Express and it was working fine. Then I have tried to publish my sample again on web server but, this time it shows an error.

Migrations is enabled for context 'ApplicationDbContext' but the database does 
not exist or contains no mapped tables. Use Migrations to create the database 
and its tables, for example by running the 'Update-Database' command from the 
Package Manager Console.

I have deleted the database on web server and tried again. but, still i am facing same error. Now, there is an empty database without table.

Application is creating database on User Registration.

I have also read this post and tried to call dbContext.Database.Initialize(true); and ((IObjectContextAdapter)myContext).ObjectContext.CreateDatabase() method but, still its not working.

5/23/2017 10:34:02 AM

Accepted Answer

First, automatic migrations are nice for development but it's a hugely bad idea to rely on them in production. Second, at some point you turned off automatic migrations, which is why your production database is no longer having tables created in it.

Go ahead and leave automatic migrations off; even in development it's better to know exactly what changes you're making to your database by generating a migration, seeing the code that will be executed, and only then running Update-Database to apply those changes.

You have a number of choices in terms of getting schema updates into production, but they all boil down to roughly the same procedure.

  1. Generate a new migration against your local database.
  2. If you want to update your production database via SQL, run Update-Database -Script. This will generate a SQL file with the code to run on the database to migrate the schema. Hand this off to your DBA if you have one or review the SQL code yourself and then apply it manually to your database via Management Studio.
  3. Run Update-Database. This time without -Script to make the changes to your local database schema.
  4. If you didn't generate the SQL file in step 2, view your local database in SQL Server Object Explorer in Visual Studio. Right-click on the local database there and choose, "Schema Compare...". Follow the wizard to compare with your production database. In the end, you can either generate a SQL file with the necessary changes that need to be made (which again, you'd hand off to your DBA if you have one), or you can migrate to production directly from Visual Studio. However, this is not really recommended. It's always best to generate the SQL, which you or your DBA can then inspect before applying the changes live.
9/22/2014 6:46:07 PM

Popular Answer

Chris Pratt is correct BTW.

I had downloaded a project in which I needed to just run "Update-Database

Here are the quick screenshots

enter image description here

Then at the bottom of Visual Studio

 PM> Update-Database


Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow