EF codefirst migrations how it works in a production environment - entity-framework

I am a newbie in Codefirst and I do not understand how it works correctly.
I created 3 migrations, migration 1..3 via "Add-Migration" command and issued the relative update with "Update Database".
I have a Configuration.cs file in my Migration directory and custom database initializer (I am working on MySQL) I created in order to seed initial data.
I do not know what happens behind the scenes in the production environment where I do not have any database yet.
Who is responsible to create and update the database? Are the migrations executed one after the other?
Can you suggest me how this process works in production and share useful links?
Regards,
Roberto

Assuming you are using Automatic Migrations - All migrations are called one after another, in the sequence in which they were created. Their Up function is called. Afterwards the Seed function on your Configuration class should fire.
Each migration has a sort of a Time-Stamp inside its file name, like this:
if you are creating lots of databases out of a single context class like I do, and you are afraid of not having control over migrations, you can use manual migrations. It is called DbMigrator.
See my/others answer here how to use it.

Related

Create Ef Core Migration-Script manually

Due to my current project structure where I need to handle many database-views I would like to write the Ef-Core Migration-Classes manually.
We are using the code-first-approach.
I know frameworks like the php Phinx where I can simply create migration files manually.
Is this also possible with ef core, without using database ef migrations add?
Update
I tried to create the migration file manually, but then it isn't executed on context.Database.Migrate().
Is that because of the missing .Designer-file or the Snapshot-File that is not changed?
Do I really need the Snapshot and Designer files? I guess the migration-system tracks executed migrations within the history database table?
Update 2
I changed from EF-Migrations to FluentMigrator. This module solves all my problems.

EF Core Migrations manual edits possible?

I am using EF Core 2.0 in my sample project with some value object configurations. I modify the code and generate migrations via CLI command line. In the last migration rather than adding a new database table as it should, it is trying to rename existing tables to each other and create an extra table for existing one. I could not figure out the reason for it.
Issue is, since with EF Core the snapshot is a separate auto-generated file from the migration itself I don't want to modify the snapshot.
I only want to modify the migration script so that it will not rename multiple tables, and then generate the snapshot from the migrations I created.
I did not see any command for this in the CLI - is it such a bad practice to modify the scaffolded migration and regenerate or am I missing some obvious new link where how to manually modify migration scripts is explained?
Thanks a bunch.
Update 1: After comments, added info about the snapshot from this link.
Because the current database schema is represented in code, EF Core doesn't have to interact with the database to create migrations. When you add a migration, EF determines what changed by comparing the data model to the snapshot file. EF interacts with the database only when it has to update the database. +
I examined my generated snapshot code from source control. It exactly has added one extra table as what I needed.
The migration script to generate this is hectic at best - renaming multiple tables to each other and then warning that this could break causing multiple issues.
Since this is a sample project for me with only mock data as of now at least, I decided to go for it and not break the automated scripts. I am willing to lose some mock data at this stage rather than wasting time on it.
If this were in a production database I would be extremely careful to manually create the same result with intervention modifying both the scaffold and the migration file.
I am accepting this one as an answer (basically saying current EF Core does not support it to the best of my current knowledge) since there is no other candidate now - I will be more than glad to accept if any better answer shows up.

Codefirst and migrations

When an application is Live an iterative approach to database changes is obviously required. In the db first world I would change the object (eg. column added to table) in the databaae project, then deploy (recreate) to my local instance, then replace the old table with the new in my edmx - when it was go Live time a delta script is generated out of the database project compared to a copy of the Live database schema. Sounds long winded but at the end of the day I only made the change once (the object in the db project) - everything else is generated
Flip over to code first (EF6) and Im expecting a similar one change experience - i.e. I add the property to the class - however do I additionally need to add a migration script ?
I've been reading and it seems many advise to disable migrations to have more control - I'm confused - I had visions of simply deploying the app and the changes automatically reflected in the target database the next time the app runs - one thing is for sure I don't want to manually write separate deployment scripts (or migration code). As mentioned I'm confused about this final part - can anyone clarify - point out the options
Many thanks
Short answer is there's no need to create a 'migration script', you're correct as EF will handle it for you if you want. I think when you read about disabling migrations, you were probably actually reading 'disable automatic migrations'; EF will still generate migrations regardless.
As you pointed out it IS a two-change process when developing: First you change your class, then you open up the Package Manager console and call Add-Migration. Usually, that's all you have to do, and EF will generate the change code for you. Then, you call Update-Database and it does it's work. When you go to deploy, you will connect to your target database and call Update-Database once and it will apply all migrations that are pending.
You can also enable auto-migrations which skips the Add-Migration step, but I always like to review the generated code. Call me old-fashioned ;)
It gets more complicated when you need support for views, SPROCS, and UDFs, but there are ways to do most anything you want to do. And, even though it's a 2 (3?) step process to get changes out to the DB, it's still much easier than changing the DB and code separately, by yourself.
Then, you can follow the steps here to set your deployment up so that once your EF is initialized on a connection to your production DB, it automatically applies the updates. Again, I would advise to do it yourself (via the package manager console) just to be safe but it's not necessary.

EF Data migrations won't detect changes when adding new migration

I am using Entity Framework 5.0 Data migrations along with code first.
When i add a new field to my model and execute the following command in the package manager console.
"Add-migration AddedField"
All I get is an empty migration called "n_AddedField", the up and down methods contain no logic.
I tried a bunch of things, reinstalling the EF nuget package, cleaning my solution, rebuilding, manually removing all generated files and directories.
Then i decided that i would scrap all my migrations and start over, and then it got weird.
After deleting all my migrations, and the migrationhistory table in the database, i recreated the database using the CreateDatabaseIfNotExists initializer. After doing this, I should be able to create a new initial migration. But when i try to create create a new migration, I get an error saying that there are pending migrations, and lists all the migrations that i just deleted from my project.
I have no idea why and how EF still has any recollection of those migrations.
I even tried searching through filecontents looking if the migrations were saved somewhere else or something. But nothing..
Data migrations look really neat when scott hansleman demo's it on stage, but for real work, I'm starting to look for alternatives.
When the project started, we were using EF 4.x and a while back switcted to 5.0, but since the switch i have added a bunch of migrations successfully.
Does anyone have any idea how to solve this problem?
Basically i just want to be able to add migrations, and generate a sql script with the changes.
I had a similar problem where a new migration was not being found, and so update-database was giving me the following error no matter what I did:
Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration.
You can use the Add-Migration command to write the pending model changes to a code-based migration.
Doing a "batch clean" solved my problem, suggesting EF was using an old/invalid assembly from a folder other than the currently selected 'solution configuration (e.g. DEBUG)'.
Hope this helps someone else out there.
oops. In my case I was adding a new root entity not referenced by any other entity. The result was simply that code first had no reason to generate a migration for the entity. Once I added the code into the DbContext (a dbset) it worked like a charm.
The problem in my case was caused by:
Create a migration (successfully)
Decide that I want to re-create it, and delete the migration .cs file
Try to regenerate it, and end up with empty migration's Down and Up functions
In this case, I forgot to also delete the ApplicationDbContextModelSnapshot.cs entries for the model changes. Removing the new mappings in this file solved my problem and it then generated correctly.
Just got the same problem but figured out that my new field was added as a member variable and not a property - it was missing the {get; set;} part and that makes migration skip that field.
May not be your case but it might help someone else.
You're 'out of sync' - Db, migrations, code - and you can expect all sorts of problems like that.
I did this million times (almost:) and it works really well - but you need to go steady, and be meticulous with what you're doing.
You can read through this 'summary' I made - start half-way somewhere (but also check connection).
Code first create tables
...and if it doesn't work I'd suggest you make a small 'repeatable' scenario / model - post exactly what you have.
How migrations work:
Migrations are tied to the 'migration table'.
When Add-Migration is run - it checks against the 'existing database' structure and migration table - and makes the 'difference' (sometimes you get none 'up' 'down' simply as too are in sync).
So, each 'migration' is a complex diff in between your code, existing migrations, and database, and migration table. Short of removing the database nothing else is certain to reset - Db 'migration' table may not be enough - that doesn't guarantee full 'cleanup' (if possible, I always do full Db delete). You also need to delete your code migrations.
Make sure to 'compile' the projects (best make them compile automatically in configuration) after/before where relevant.
Make sure your 'connection' matches.
Once all is in sync - all should work nicely - but you gotta keep it in sync. Unless you plan to delete the Db (testing) - don't remove migrations just like that (you can use Update-Database -0 (I think) to get back to some migration (this is 'zero state').
I had a problem similar to this, where using the -force flag on add-migration to re-scaffold an existing migration stopped working for no apparent reason.
No matter what I did I got the stupid "Unable to generate an explicit migration because the following explicit migrations are pending" error message. After trying nearly everything I could think of and stopping just short of smashing my laptop to pieces, out of desperation I ran enable-migrations again and of course got the "Migrations have already been enabled in project 'Blah.Blah'" message. Tried add-migration -force again and magically it was working.
I have no idea what it changed- must have been some user settings/config file outside of source control. Hopefully this will help someone else.
The batch build -> clean option did not work for me.
I solved the problem by:
Creating a migration with 'Add-Migration NameOfMigration'
Deleting the contents of the up and down functions of the created migration class.
Updating the database by running the migration script (which will just add a row to the _MigrationHistory table with 'Update-Database -Verbose'
The web application now runs successfully, so essentially I had an issue that was fixed by adding meta-data only.
It happened to me and nothing worked. Then i did this on my own and everything works now.
Problem:
I created a Model "Cars". And When I create a migration for it using command "add-migartion AddCarModel", a migratoin was created but it was empty. I tried with different names and also tried delete migration's .cs file but nothing worked. Then I did the following:
Solution:
Follow below steps:
1. Delete all the empty migrations that you created for the Model. (But remember the names of the migrations for step 2)
2. Also delete those migration entries from "_MigrationHistory" table.
3. Comment out you line(s) of your model DB context, (in my case it is "public DbSet Cars{ get; set; }")
4. Clean and Rebuild solution. (Its best that if you batch clean)
5. Make sure that your update command is working and not throwing errors. (Command: "update-database -verbose")
6. Now uncomment line(s) that you commented in step 3.
7. Now create the migration for that model. (I created the migration with same name as before)
Hopefully it works. :-)
I added a new class to my data model to a sub-directory, the resultant namespace was not visible to scaffolding using add-migration.
Fix was to rename the namespace of the new class to conform to the rest of model, and/or add "public virtual DbSet .." etc into your entity context class, which will require you to reference this new namespace, then run add-migration again.
It seems that i managed to solve the problem by moving the models and the context class to another project.
I still have no idea why this happened, and this solution is really no solution at all :(
I had the same problem. Migrations were enabled but they weren't detecting any changes.
My solution was to re-enable migrations using -Force attribute and then everything worked.
Enable-Migrations -ProjectName -StartupProjectName --ConnectionStringName -Force
I had to delete the _MigrationHistory table that is generated by EF. Then I ran add-migration again. Be careful with this though, as it will generate the queries needed from scratch, including tables that are already there.
In my case it was because I had added a secondary context 'ApplicationDbContext' as part of the ASP.net identity stuff. When I ran the 'enable-migrations' command again I got an error that there was more than one context. Once I combined the two things started working again.
Maybe the stupidest of all:
I was adding a migration with the same name as the new object I was creating.
I had to run dotnet ef migrations remove even though I'd deleted the previous migration
If you are using fluent api to set up your configurations for the DbSets then you won't have any problems with it

Entity Framework 5 Code Migrations

Where is stored information about current database state (what migration is applied)? I suppose it can be "dbo.__MigrationHistory" table or this table is just for logging purpose?
If I enabled migration, added migration and updated my database. After that I checked-in code to SVN and another developer checked it out. What this another developer has to do to create/update his own database?
I see such options:
1) Call Update-Database command right away.
2) Do everything from beginning (Enable-Migration, Add-Migration, Update-Database).
3) Do everything but skip Add-Migration step (it is already present and it seems strange to add it once again for every new developer).
Which of my assumption is right or if no one where is the right way?
To retrieve which migrations have been applied to the database, you can use the Get-Migrations command (reference).
Everything depends on how the database is created, which initializer you are using.
This article is worth reading if you are unfamiliar with those.
When using the DropCreateDatabaseAlways initializer, you don't really need to care about updating your database, because your database will be deleted & recreated at each application startup.
When using the DropCreateDatabaseWhenModelChanges initiliazer, your database will be dropped then recreated if EF detects the model has changed at the application startup.
When using the CreateDatabaseIfNotExists initilizer or if no initializer has been defined, your database will be created if it does not already exists. If the database already exists & you added a Migration, you (and every developer retrieving your code) need to use the Update-Database command to update the database.
There is a new initializer introduced with Code-First Migrations: MigrateDatabaseToLatestVersion, this initializer automatically update the database to the latest Migration defined.
See this page's last section: http://msdn.microsoft.com/en-us/data/jj591621.