I am using EntityFramework CodeFirst, I have added a relation to one of the tables but when I am trying to update the database from package manager console, I need to add -verbose flag but I have data on my table and need to keep them. Is there a way to change the database without losing data?
Thanks.
You need to manually edit the Up and Down methods of the migration to add relation on the database by adding t-sql commands.
Related
I'm trying to connect my app to an existing Application.
So i created the Entities with the Entity Framework Database First.
It create all my entities and my Context that's fine.
But when i try to run it tell me that my migrations are not up-to-date so i tried to add-migration test to see what was missing...
It's creating all the table i asked it from DataBase First...
I cant recreate those tables i just want to be able to connect to those tables...
I absolutly need to connect to those tables, i cannot create a new one and i cannot clone the database.
How can i achieve this... i didnt find any resources on the subject that goes from the start to the end of the process.
Some help would be awesome!
The Problem: You can't reset migrations with existing tables in the database as EF wants to create the tables from scratch.
What to do:
Delete existing migrations from Migrations_History table.
Delete existing migrations from the Migrations Folder.
Run add-migration Reset. This will create a migration in your Migration folder that includes creating the tables (but it will not run it so it will not error out.)
You now need to create the initial row in the MigrationHistory table so EF has a snapshot of the current state. EF will do this if you apply a migration. However, you can't apply the migration that you just made as the tables already exist in your database. So go into the Migration and comment out all the code inside the "Up" method.
Now run update-database. It will apply the Migration (while not actually changing the database) and create a snapshot row in MigrationHistory.
You have now reset your migrations and may continue with normal migrations.
I love code first. But here and then facing contraint problems and such , that makes me drop database and recreate etc.
My concern is that, once in production, i cant do that.
Is it possible not to use code first migrations and just creating fields by hand at database/tables?
That also makes me worry for foreign key constraints again.
What is the best way to do this?
If you use EF < 4.3 you can recreate database on dev server and manual update production database in two step:
creating fields by hand at tables
update EdmMetadata table (use data form dev database)
Else use code first migrations : update-database -script,
This command generate update script for you database, and you can do it manually.
At about 4:20, the tutorial video at the Microsoft website starts talking about Migrations
http://msdn.microsoft.com/en-us/data/jj193542.aspx
Is it okay to have a table in the database that is not one of the entities when using code first with database migrations? Or will this interfere with the migrations? I want to put in a table to track some miscellaneous information.
We do this with no issues. We do add the table through the migration though so we we can ensure all developers have the same schema. If you want to skip this and go via SQL Management studio then you should have no issued either.
Sql(#"CREATE TABLE.....")
Is there a way to delete an automatic migration in the migration history of a project using Entity Framework Migrations? This is a code-based EF project that's has a mix of explicit and automatic updates. There is an automatic update that deletes a table of content and isn't necessary. I'd like to be able to just remove it.
For some reason, the database it's running against doesn't have a __MigrationHistory table. (At least I don't see it in the list of tables for the databases or the list of System Tables in the database. Not sure if you have to enable viewing those somehow or not inside SSMS).
Please offer any advice you have. I'm open to whatever solution as long as I can maintain all that application's data.
I hope it is not too late.
The __MigrationHistory table is made in System Tables section on Target Database(Same as base mode Database)
I have my database already set, I want to add a new field to the model, a new column to the table, is there a way I can do this without losing all my data?
Normally if you delete the DB it will recreate everything automatically but I don't want to lose the data.
I'm using SQL Server 2008 as database.
You will need to use EF Migrations to add the new column to your database. You can read more about EF Migrations here and here.
If you're using code first, I've always just added the column to the database manually in situations like that. Unfortunately, there is no simple way to automate incremental model updates with Code First.
For example, one of EF Code First's own errors even specify manual update as the best option:
The model backing the ‘your context’ context has changed since the database was created. Either manually delete/update the database, or call Database SetInitializer with an IDatabaseInitializer instance. For example, the DropCreateDatabaseIfModelChanges strategy will automatically delete and recreate the database, and optionally seed it with new data.