I have been using ef code first approach. I can apply model changes using migration which was came with 4.3 of Ef. I want to migrate new model changes to test db when tfs build deployment is running. How do I?
Best regards.
Usually migrations work like that:
First. You enable migration via PM console:
PM> enable-migrations
Second. Then you add some entities to your context, and add migration:
PM> add-migration Blabla_adding_Entity
Third. Then you can apply it to the database:
PM> update-database
If you want your migrations to be automatically enabled, just set this option in your Congiguration.cs:
public Configuration()
{
AutomaticMigrationsEnabled = true;
}
Related
I am using Entity Framework Code first in my application. But while using this after updating model, every time I have to enable automatic migrations and run update-database using package manager console. Does anyone has solution on this, whether we can automate this and update-database without using package manager console.
You only have to run Enable-Migrations once, to generate the Configuration.cs class and the Initial migration. After that, whenever you change the model you have to generate a new migration by running Add-Migration, and also you have to run Update-Database to apply the migration to your database, but you shouldn't need to run Enable-Migrations again.
If you want to automate the process you can enable the automatic generation of migrations by setting AutomaticMigrationsEnabled = true in the constructor of your Configuration class, and use the MigrateDatabaseToLatestVersion database initializer in your DbContext. This would allow you to just change your model code and not having to do anything with Package Manager Console, no Add-Migration nor Update-Database required. You still need to run Enable-Migrations once, at the beginning, after you create the project, to generate the Initial migration.
Anyway automatic migrations can cause some problems, I wouldn't really recommend you to use them. So I would keep on doing the manual Add-Migration commands whenever your model changes. You can still skip doing the Update-Database if you use the MigrateDatabaseToLatestVersion database initializer.
Another way to do it would be using a DbMigrator to run the equivalent to Update-Database from your code (but you don't need to do this if you are using the MigrateDatabaseToLatestVersion database initializer):
var migrator = new DbMigrator(new DbMigrationsConfiguration());
migrator.Update();
I have created a code -first entity framework migration and applied to the database, is there a way I can rename this migration ?
You cannot rename after applying a migration to Database. But you can delete and add another migration with same name or other name.
First reset the migration to last migration
PM> update-database -TargetMigration:{lastmigrationname}
If this is the first migration then
PM> update-database -TargetMigration:0
Delete the existing migration and relevant files from the source code
Add the migration using the name you want
PM> add-migration {MigrationName}
Then update the database with new migration created
PM> update-database {MigrationName}
In my project I have dozen+ migrations. http://screencast.com/t/CA2kZk3WCFj
But when I try to create database from scratch EF only starts from the marked migration to the bottom.
if I enter command:
update-database -targetMigration InitialCreate
this is the response:
he specified target migration 'InitialCreate' does not exist. Ensure that target migration refers to an existing migration id.
How can I resolve this issue and make EF see all of the migrations?
This can often happen if you use the Update-Database command in Release mode. Running in Debug mode seems to work ok.
I had the same problem.
In my case, the "ignored" migrations were not in the same namespace (caused by a project renamed).
I work on a team of 4 developers using EF5, everyone working on their own local database. Up until now we've been using automatic migrations but we're nearing the point where we need to release to production so we've disabled automatic migrations and started adding explicit code-based migrations.
Here is the problem: I ran the Update-Database command after a developer created a new explicit migration and I get the following error:
Applying code-based migrations: [201209080142319_CreatedDate.LastModifiedDate.Additions].
Applying code-based migration: 201209080142319_CreatedDate.LastModifiedDate.Additions.
Applying automatic migration: 201209080142319_CreatedDate.LastModifiedDate.Additions_AutomaticMigration.
Automatic migration was not applied because it would result in data loss.
Why do I get this error even though I've disabled automatic migrations? I can fix this error by deleting the explicit migration and then re-scaffolding it (running Add-Migration). Then Update-Database runs fine and doesn't mention anything about 'Automatic migration...' Also, the code in the migration created by me when I run Add-Migration is identical to the one created by my teammate. I don't see why it would even try to do an automatic migration since AutomaticMigrationsEnabled = false;.
What am I missing here?
I hate to answer my own question but I encountered this problem again. A developer on my team re-enabled automatic migrations on their local machine and then created an explicit migration, which reproduced this behavior as soon as I ran it.
Entity framework will always run an automatic migration before it runs an explicit migration that has the Source property defined in its .resx file, even if AutomaticMigrationsEnabled = false. An explicit migration will only have the Source property set if it is created after an automatic migration has been run.
The upshot is that disabling automatic migrations only means that EF won't automatically upgrade your schema when it detects model changes - but it might still do an automatic migration if it needs to fill in a gap between some explicit migrations. To avoid this behavior, don't use a mixture of automatic migrations and explicit migrations.
public class Configuration : DbMigrationsConfiguration<bailencasino.com.dal.Context.BlncnoContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
AutomaticMigrationDataLossAllowed = true;
}
Add AutomaticMigrationDataLossAllowed = true; so you assume that you want to allow EF to add remove SQL objects that result in data loss.
My team has experienced something that may be related to this. If two team members both add a migration, check in their code, get latest, and then perform update-database, the second one will get an error because there is a migration "skipped" - their system sees the team member's migration was never implemented.
We've started checking everything in and getting latest, doing the update-database (if a team member added a new migration), then doing add-migration, update-database, check-in.
AutomaticMigrationEnabled = false prevents your application from updating the database on its own.
But since your running Update-database yourself, Update-Database checks the current state of the database and then runs all the migration steps not already in the database including the changes in the model (dbContext) that do not have a code-based migration yet.
My guess is that there is a change in the model that would cause data loss.
You can use the -force parameter to apply the changes still when there is data loss.
#Dave Graves is right. I had the same issue and did his fix in order to disable the auto migration of particular migration file
heres some instructions:
go to where your migration files are located in solution explorer (if you are using visual studio)
look for the migration file causing that auto migration e.g: 202008181102535_BlogPost.cs
expand it and you will see its designer file. open that file
inside that file, you will see the Source property of it is being set by something like Resources.GetString("Source"). replace that with null and then try again to manually execute update-database in your package manager console.
For some reason, my migrations appear to have been jumbled/corrupted/whatever. I'm at the point where I just want to start over, so is there a way to completely undo all migrations, erase the history, and delete the migration code, so I'm back to square one?
e.g.) PM> Disable-Migrations or Rollback-Migrations
I don't want to "update" to an original migration step (i.e. something like an InitialSchema target) because I can't find it anymore.
You can rollback to any migration by using:
Update-Database -TargetMigration:"MigrationName"
If you want to rollback all migrations you can use:
Update-Database -TargetMigration:0
or equivalent:
Update-Database -TargetMigration:$InitialDatabase
In some cases you can also delete database and all migration classes.
For Entity Framework Core:
Update-Database -Migration:0
Remove-Migration
To be clear, if using LocalDb, when you want to start from scratch just delete the database via the Database Explorer and then type enable-migrations -force in the Package Manager Console. Do not delete the database via the App_Data folder or you will have the following issue.
Update-Database -Migration 0
Remove-Migration
The documentation is here: https://learn.microsoft.com/en-us/ef/core/miscellaneous/cli/powershell#update-database
and here: https://learn.microsoft.com/en-us/ef/core/miscellaneous/cli/powershell#remove-migration
It is written wrong in their documentation i guess , for me i used
Update-Database -Target MigrationName