Code First Migration - entity-framework

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).

Related

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.

EF5 Code first migrations reset migrations

I was using EF5 code first and migrations right from the start.But I messed up something
and decided to reset my migrations.
I did enable-migrations -force
Then tried add-migrations xyz
I expected only to see incremental changes (addition of 2 tables)
Instead it tries to recreate every table.I dont want this to happen as it is shared via GIT and I need to push the migration also.
I have deleted all migration history and folder.What I want is a way to do another migration and it should only do the incremental create tables as other tables are already there
You need to do this in two steps:
Comment out your added tables and create an empty migration that updates the meta data to the state before those where added, by using the -ignore-changes switch (see my command reference)
Readd your tables and create a migration. Now it should contain only those two new tables.
Please note that whenever you mess manually with the migrations you need to be careful with the state of the metadata or you can get really nasty surprises.

Restoring tables in entity framework

I am working with EF very first time. For some reason I was having difficulty to run my application and every time I was getting exception that your model is not synced up with the current database. I tried everything and since data wasn't important, I thought to re-create all tables by running all the migrations. So, I removed all tables and tried to run the following command. I am using model based approach here to define my entities.
Update-database -targetMigration:InitialCreate -verbose
I was under the impression that it will re-create all tables starting from the very first migration but it actually reverted all to that very first point. Now, whenever i run update-database command, I always get the following
No pending explicit migrations.
I am very confused what should I do to take my table generated back and set my app in working condition. Can anybody help?
Thanks
-Fahad
If you are using model first then you can click on the Model whitespace and do "Generate database from Model". If you are doing code first, EF creates a table on the database called "__MigrationHistory". If you delete that table code first will recreate the database model from your current code based model.

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.

how does the Table _MigrationHistory under the CODE FIRST in the Entity Framework create? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
EF (Entity Framework) 4.3 Migration tool does not work on EF 4.1 DB
when use the entity framewrok code first mode to build database, a table '_MigrationHistory' of system table type will be created. how do I manually create table like that? the code first tutorial show us how to code first to build a new database,not on an exist database, and when I want to use the database migration function of the code first on existing database , I found I failed. and NO table named '_MigrationHistory' created. and I found a solution 'http://thedatafarm.com/blog/data-access/using-ef-migrations-with-an-existing-database/' can resolve my problem, but there is a step need to copy(should be 'create' here) the table '_MigrationHistory' to the existing database which is object of system type, how do I create a table like that?
I WAS POOR IN ENGLISH, MIGHT MY EXPRESS MAKE YOU CONFUSED. HOEP NOT.
You don't need to create __MigrationHistory manually. You just need to create initial migration. The initial migration will create the table for you. This command will add initial migration called InitialMigration and ignore all entities you have in your project:
Add-Migration InitialMigration -IgnoreChanges
It means that you should run this command when your classes in the application reflect your initial database. All changes which should be handled by migrations must be done after creating the initial migration. Here is the whole tutorial.