I have been made a few changes to my database locally and have been adding migrations and updating the database via the Package Manager Console.
Now I've checked in and deployed out to the dev server however I am getting the error:
The model backing the 'MyProjectContext' context has changed since the
database was created. Consider using Code First Migrations to update
the database (http://go.microsoft.com/fwlink/?LinkId=238269).
I know this is telling me that my version of the database is out of date however I don't want to drop the database this time so im trying to generate a script to manually run on the database however I don't fully understand how I can tell it which is the last migration that the dev db is aware of and which is the current, up to date one, ive tried the below however its not working:
So If "Added IsImportant to exceptions" was the last migration updated on the dev server, and "Set ImportantId as Identity Specification" was the last update I manually run locally, how do I generate the right script for dev?
Turns out I was doing it right but needed the full configuration name with double quotes.
Update-Database -Script -SourceMigration:"201507091527309_Added IsImportant to exception" -TargetMigration:"201507281410132_Set ImportantId as Identity Specification"
Once I ran this it generated the script and I was able to upgrade the database.
Related
I am using Entity framework and code first approach in my project. I have done some Migrations to my server database and it was working fine. now I added a few more tables and try to create a migration by add-migration command to the database on server.
The command runs with no error and created a migration file.
But, when I check the migration file the code in the migration file was to create all tables again to database (create table for already created tables are also in the up method).
When I run Update-database command it returned an error saying
...table already exist in database.
I try it on some local database on my system and it was working fine.
Can anyone help me with this?
Is there any special permission to set in the server?
Is it because some services from visual studio getting blocked? If so how can I solve it?
When trying to run my ASP.NET MVC application locally, everything works fine. But once I've deployed it Azure, it keeps giving me the error:
Unable to update database to match the current model because there are pending changes and automatic migration is disabled.
So I connected to the remote database using the Package Manager Console in Visual Studio and ran the Update-Database command. This gave me the same error. When I look at the database inside the SQL Server Management Studio, it looks like all migrations are applied.
So after this I tried running Add-Migration to see if there were any changes I didn't notice. The migration it created was exactly the same as the previous two migration (there is nothing in there that's not in one of those migrations).
So far the site ran just fine for about two weeks, including a few migrations. I'm at a loss as to how to solve this, so any help is appreciated.
I'm certain I'm connecting to the right database, because I see it change when I update it to a target migration. I'm also targeting the correct project.
If you are sure that the Add-Migration is creating the script for changes that are already applied, you need to update the migration state of your database. You can do this by adding an empty migration. This will capture the state of your current model.
Add-Migration MergeChanges –IgnoreChanges
After running this command you will have an empty migration script. Now you can update your database to match the model state. Since the actual migration doesn’t contain any changes, it will simply add a row to the __MigrationsHistory table indicating that this migration has already been applied.
NOTE Run this only if you do not have any pending changes and yet you see the error message to update the database.
I have a C# solution. Recently, we moved the solution into a new repository.
In the OLD solution, I have a soon-to-be deprecated console app that accesses my Azure-hosted DB to do some work. We had some new DB migrations in the new repository and had some work that required the console app, so I moved the app project over into a throwaway branch in the new repo just so I could build it with the latest migrations.
It compiles fine. However, when I try to run it, I ALWAYS get the The model backing the 'DataContext' context has changed error.
What's weird about this is that my code and my DB are on the same migration. I can even run the update-database command on that DB and it tells me that I have "no pending migrations". I did compares on the stored procedures in my visual studio and the DB and they are all equal. Same results when trying Add-Migration "SyncTest", it creates a migration with no changes.
I cannot delete the __MigrationHistory table because I need to run this console app on production servers and cannot afford to lose that data/have any downtime.
So if I am on the same migration step between my console app and my DB, then why is it still throwing this error? I dunno where else to check!
I don't know why it is happening in your particular case, but I did stumble across a way to turn off the error, put this line in your DbContext instance ctor:
Database.SetInitializer<YourDbContextClass>(null);
I have a WebApi2 project that I'm deploying to my development server via a web deploy package. I used the WebApi scaffolding with MVC, and did successfully create an initial migration for the user-related tables.
Everything worked fine until I accidentally added a controller with EF & actions, using the wrong model. I deleted the controller, but something must have been created by EF behind the scenes, because now every time I try to access the Api I get the following error:
The model backing the 'ApplicationDbContext' context has changed since the database was created.
Consider using Code First Migrations to update the database.
I looked at the Migrations folder, but there are no changes in there.
I checked my source control, but I can't see any changes in there, either.
I have tried setting AutomaticMigrationsEnabled = true in my configuration class, but that didn't seem to do anything at all.
I tried calling Update-Database from the package manager console, but that's looking for the Db in the App_Data directory, whereas the correct Db is hosted on my local instance of SQL Server, so that failed.
I even tried to add a new migration, but that fails with the following error:
Unable to generate an explicit migration because the following explicit migrations are pending: [201603181320388_Initial]
Looking at the database I can see the tables that were created by that migration, so I'm pretty sure that's also due to EF not seeing the correct Db.
I tried changing the connection string in my project's web.config file to the one from the deployed web.config file in an attempt to make EF look at the correct file location, but no joy.
I'm pretty new at using EF, so the answer is probably something obvious, but I can't see it.
Is there maybe a way to see what has been changed in EF and undo it? Or can I specify the path to the database for the Update-Database command?
I my MVC 5 project I used code first approach for creating the database. However after enable migration, I am getting 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.
However if i delete migration folder then error is resolved but then I an unable to use migration. Please help. I am attaching screen shot for the error.
I run the command for add-migration and then update-database. That did help me. But if i change connection string and run the project again then it is not creating database until migration folder is not deleted.
Before you can use your database, you need to create a migration, that describes structure of your database with the Add-Migration command and then update your database to match you current model with the Update-Database command.
You might find this tutorial useful.
Enable migrations just creates scripts for creating and seeding that database. You need to run update-database from the package manager console to create the database. The database will be created on the server referenced in your web.config connection strings