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.
Related
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.
I've set up this project from DB first and everything went fine. I can debug properly. But when I try make a change to one of my models, instead of the new migration showing a simple AlterColumn statement, it keeps trying to reinitialize the entire database.
I've tried running update database -script and only inserting the _MigrationHistory table record. Even after that, it still wants to create all the tables.
Has this ever happened to anyone?
When you start with an existing database you need to do an empty (no-op) migration to set a baseline. This is because EF will use the model in the prior migration to compare, so if there is not one (in code) you get all your database objects. Inserting a record into __MigrationHistory just tells EF the code migration has been applied - it doesn't use it for the compare.
enable-migrations
add-migration MyBaseline -IgnoreChanges // no Up() code, but model saved
update-database
// Now I can change my model and generate a migration with difference
See here.
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).
Desired outcome:
Use model first approach with Entity Framework and allow changes to deployed database/ model to be done automatically based on the changes in the model. Automatic schema difference script generation to allow smooth migrations.
Is there a way to perform migrations in model first EF6? I can see code first migrations topics all over, but nothing much on Model First.
Options I saw so far:
Database generation power pack (seems outdated)
somehow convert to code first, then use migrations (not desirable, as I like to have a visual designer)
somehow piggy back on code first migrations (http://blog.amusedia.com/2012/08/entity-framework-migration-with-model.html : this is for EF5, got error that can't run migrations on Model First)
some third party tools?
As far as I know there still is no automatic migration for Entity framework model first.
Our approach is:
Create a fresh database from the model.
Create a diff script to migrate the old database to the new one.
Verify that this diff script is indeed correct. Always double check what your automation tool creates.
We first used Open DB diff for our model first migrations. After that we switched to Redgate's SQL compare because it produced more reliable migrations .
In our experience DbDiff produced a lot of unnecessary SQL because it bothers with the order that columns are in, and has some other issues like foreign keys constantly being dropped and re-added. Aside from that it still did the job fine, but we had to do a lot of double checking on its generated SQL.
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.