update-database -force command is not updating the base in entity framework code -first

asp.net-mvc ef-code-first entity-framework sql-server

Question

May be this question seems duplicate. But here are the issue that I am facing with the command.

If I run this command,

update-database -force

the first error I gets..

There is already an object named tblAbc in the database

Then I googled and everytime I get a stackoverflow link for suggesting to run Add-Migration Initial -IgnoreChanges

If I run this command and then run the update command, No error - Running seed method

But it doesn't sync the database with the new updates

Then I tried for number of times but same issue, hence I looked for some alternate solution & I got.

to use update-database -Script but if I run this command and get the following error.

User canceld Save dialog box

Then I tried to fix this by opening SQL Managment Studio and

Tools>>Options>>designers>> unchecked the Prevent saving changes that required table recreation

But after this also same error message.

Then I tried to add the following coniguration in my context class

System.Data.Entity.Database.SetInitializer(new MigrateDatabaseToLatestVersion  <context, Configuration>());

It ran with success and Running Seed method but it doesn't sync the latest updates.

1
4
11/4/2015 9:20:53 AM

Accepted Answer

Using an Existing Database

Depending on the state of your context you need to the one of the following...

IF YOUR DATABASE CONTEXT CONTAINS ONLY YOUR EXISTING TABLES AND NO CUSTOM CHANGES DO THE FOLLOWING

Add-Migration Initial -IgnoreChanges

This will create a blank migration script

update-database

This will update the database to this migration but no changes will be applied. You can now add your changes to the database context. After you have finished do the following

Add-Migration Custom

This will generate a migration script with your changes in it. Then update your database again.

update-database

IF YOUR DATABASE CONTEXT CONTAINS YOUR EXISTING TABLES AND ALSO YOUR CUSTOM CHANGES

Add-migration Initial

This generates a migration script. Go through the migration script and remove any references to existing tables in both the UP and DOWN methods. You will be left with a script which only includes your custom logic.

update-database

Hope this helps!

6
11/4/2015 10:07:33 AM


Related Questions





Related

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