I can't find official documentation of this method.
All I found is that it apply migrations that didn't applied yet (and create db if not exists).
but how it work?
is it look at the db Migration History table to see which migration missed?
and what if somehow it see that Migration History table has more migrations than in migrations folder? is it downgrade them?
thanks!
Does it look at the db Migration History table to see which migration are missing?
Yes, and applies any missing migrations in chronological order.
What if somehow it sees that the Migration History table has more migrations than in the migrations folder? Does it revert them?
It does nothing. They might be for a different model/DbContext/application that is using the same database.
Related
I have an issue where I have an existing database which where I have mapped over only certain entities to use with EF Core (the DB has 100s of tables and I dont want classes for all of them.
I scaffolded the starting stage like this
dotnet ef dbcontext scaffold .. etc etc
Where I specified specific tables with the connection string.
I then created an initial migration to snapshot the starting stage with
dotnet ef migrations add Initial
I now want to add a new table based on a new entity. So I created a second migration using
dotnet ef migrations add NewTable
But when i run this using..
dotnet ef database update NewTable
It attempts to add the tables in the initial migration! because I havent ran that migration, so I have no records in __EFMigrationsHistory. But I dont want to run the first migration as I already have the tables which were scaffolded! Initial was just to create a snapshot which is what I thought you have to do.
So is there some way to make it think Initial has already been run? By putting something in
__EFMigrationsHistory? Then I can run the NewTable migration without getting "object already exisits".
Thanks
It ran my latest NewTable migration fine after I removed the previous one, otherwise it was still trying to add objects from the initial migration resulting in "Object already exists".
I find this strange as the statement
'dotnet ef database update NewTable'
specifies which migration I wanted to run, and shouldn't run initial again. Anyway this fix worked fine.
I'm working on an ASP.NET 5 app that uses Entity Framework 7 with migrations to alter the application's Microsoft Sql Server database.
I'm running into a few issues when I reach the migrations step, and I would like clarification on what the Entity Framework commands migrations add and database update do.
It's my understanding that
> dnx ef migrations add Initial
creates a C# file ending in the name Initial in a folder named Migrations containing code that will create tables based on the application model classes, and
> dnx ef database update
executes the code that will add those changes to the database. However, after the migrations add command, the database has already been created, and the console gives an error when I run the database update command, saying that the tables already exist.
From what I've read on different tutorials, it seems like migrations add shouldn't actually affect the database, and the changes would take place when you run database update, but it doesn't look like that's the case.
Can someone explain what each of these steps are doing and how they fit together? Thanks in advance!
dnx ef migrations add shouldn't create the database. I suspect DbContext.Database.EnsureCreated() is being called somewhere in your application code giving it this effect.
ASP5 Web App projects come with a Migrations folder. What exactly is the migrations folder for? As far as I got it is for database migration but how do I use it ? Is it safe to delete it ?
This is actually a feature of Entity Framework and has been for a while. Also known as Code First Migrations.
In my application I enable Code First Migrations with some migrations, Also I use SQL Server Compact for integration test.
When I run my tests, Entity Framework create an empty database and tries to run migration on that empty database and thrown The specified table does not exist.
Based on this report I think usage of Migration in Entity Framework 6 has changed.
I test all Database Initializer with Context.Database.Create(); but in all case tabale's never created.
I don't know that this is EntityFramework's bug or not, but when I made rename the namespace of Migration Configuration class from default (Projectname/Migrations) to any none default name, migration works well.
Context.Database.Create() will not execute migrations! It only creates empty db. To Update database from code to latest version you need to use DbMigrator.Update method:
var migrator = new DbMigrator(new MyMigrationsConfiguration());
migrator.Update();
Alternatively you might use MigrateDatabaseToLatestVersion
Database.SetInitializer(new MigrateDatabaseToLatestVersion<BlogContext, Configuration>());
It is described in details here: http://msdn.microsoft.com/en-us/data/jj591621.aspx#initializer
In case someone still struggles to fix the issue.
The code that follows works for me: add-migration MyFirstMigration
Meanwhile add-migration "MyFirstMigration" with the migration name ramped in quote doesn't work.
There may be previous migration files which the ide may be referring to mostly likely due to caching.
Drop backup and drop target database if it exists, and drop the migration folder.
Now add the migration and you will be good to go.
It does happens when adding model and running add-migration command.
Here is the simplest cause of this issue:
Add a newly added model property into IdentityDbContex class.
Here are the steps:
create model
add property into IdentityDbContex class
run add-migration
update-database
We are using Code First migrations and apply them using DBMigrator.Update() from our Application_Start event. Recently when deploying a new migration we got the following exception in from Application_Start:
PK_dbo._MigrationHistory'. Cannot insert duplicate key in object
'dbo._MigrationHistory'. The duplicate key value is
(201312020928218_ModifyReport)
This suggest that 1) Application_Start ran more than once and 2) code first migrations do not use a transaction to prevent the same migration being attempted multiple times. If that is so should we move migrations out of the appplication completely and include as part of deployment instead?
Code First migrations do not use transactions.
I guess you could customize migrations to use them, at least in EF6.
I'm kinda battling with similar issues myself at the moment.
Other than that I would put the update as part of the deployment rather than it being part of the app.