Entity Framework 5 Code Migrations - entity-framework

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.

Related

entity framework core migration

I'm new to Ef core. Using migrations. I made some changes in my code recently.
Got following error:
The operation failed because an index or statistics with name 'IX_JobPosts_Role_Domain1_Filters_Item1_ExperienceLevelId' already exists on table 'JobPosts'.
Ok, so I did what I usually do when I encounter errors, delete my database and previous migrations and start all over again.
First migration works fine everytime. Second migration prompts the same error even though no changes has been made to my code.
Why does it keep adding columns that already exists?
the reason is that ef core does not support nested owned types.

EF 6.1.3 automatic migrations

I created an initial migration into my database when I executed a database operation via code first.
All the tables got created successfully.
I have successfully enabled automatic migrations.
I've added a couple of columns to a model.
When I run the "Update-Database –Verbose", I get an error message (which I understand, but don't now why it's coming out) of "There is already an object named 'Categories' in the database."
The Categories table along with all the rest of the tables got created on the initial migration (as explained above).
I made a change (adding the two other columns) to another model (Customer).
I would think, that the Code First migration would simply look at the entire DBSet model, determine what changed, then simply apply the necessary SQL. Instead, it's trying to create the Categories table again which already exists.
Can someone please explain to me how I can simply update the DB without this happening? What am I doing wrong or what do I need to do in order to get it to work....
EDIT
Successful creation of EF code-first Migrations setup:
Enable-Migrations -EnableAutomaticMigrations
Add-Migration Initial (Creates _Initial.cs file).
Comment out code in _Initial.cs file within Migrations folder for
code that you don't want to execute AFTER initial DB run to create tables.
Set Configuration.cs file to Public access
Add following code in Global.asax file
Database.SetInitializer(new
MigrateDatabaseToLatestVersion());
Update-Database - Verbose (for any DB changes)
Set "AutomaticMigrationDataLossAllowed = true;" in Configuration.cs
file within Migrations folder. (for any DB changes)

EF codefirst migrations how it works in a production environment

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.

Code First Migration

I have read the code first migration from msdn.But in case If my database team changes data base i.e. If some one add/update/delete some fields from a table how to migrate that changes?
I mean How my code first model update that change?
Please help me.I am using Entity Framework 6.1.
I didn't fully understand your problem because the real purpose of migration is Update Database changing fields or tables. You just need to launch:
add-migration NameOfMigration
and
Update-Database.
in package manager console.
As you can see in the migration called "NameOfMigration" that is created you have 2 different method: Up and Down. This allow you to Update your database with that changes (up) or come back (down).

Migrating from EF5 to EF6 with existing migrations

Context:
I currently have a system built on Entity Framework 5, which we'd like to upgrade to 6.
However, it already has dozens of migrations applied. And not just that, we have unit tests that verify all migrations by creating a test database and updating to latest and back to initial, to ensure all Up and Down migrations can be properly applied.
From what I've been reading, if migrations have different EF version numbers, Update-database fails to cross that boundary, meaning, the unit test covering all migrations would never pass. I could be mistaken and EF migrations might be backwards-compatible.
Question:
In that scenario, would it be wiser to actually merge all old migrations into one large InitialCreate migration (recreated using EF6), deleting the MigrationHistory table and "fake-apply" the migration to the live database (by commenting out the code of that migration temporarily) to get the new history entry?
And second of all, is this something we'll have to do again when updating EF in the near future? If that's the case then it might seem like EF is missing some support regarding cross-version migrations.
I ultimately ended up implementing what I described above in the question itself. All this was done AFTER migrating everything to EF6.
In my case I not only needed to apply this to my local database but to a live database as well.
The steps I used to accomplish that where the following (hopefully I'm not forgetting any, since this was a while back):
Backup your databases (both local and live), just in case you need to undo this.
First we need to create one merged migration for the entire database.
Change your ConnectionString to point to a new blank database.
Physically delete all migrations from your solution, as well as your initial creation one.
Run Add-Migration InitialCreate, which should add a migration to regenerate the entire database.
Now you don't really want to run that migration. In my case I only need that for unit tests, or to create new databases from scratch.
So then we continue:
Change your ConnectionString back to your local database.
Physically delete the MigrationHistory table in the database (or possibly just remove the rows, I can't recall exactly).
Comment out ALL the code whithin the InitialCreate migration, to make sure it will do nothing when applied.
Run Update-Database, which should add an entry to the MigrationHistory table simulating an initial creation of the database.
Uncomment the code in the InitialCreate migration.
The same process can be applied for the Live database. You can point to it in the ConnString, manually remove migration history, comment the implementation of the migration and apply it, to simulate creation, then uncomment.
From then on, the database and migrations should be in sync, and unit tests (on a separate database) should be able to call Down() and Up() on all migrations and still function properly.
I had also done same thing today (5-May-2014) and did not face issue mentioned by you
and had used steps suggested in
http://msdn.microsoft.com/en-us/library/upgradeef6.aspx
so my old migrations still remains the same.
Though i faced some other issues related to
- Miniprofiler (need to use new with EF6 support)
- and one issue related to re-creation of all index after upgrading.