0

I'm working on an Entity Framework project (worked fine), I accidentally pulled an old database from production on to the project (unable to revert back). The DbContext has changed since. The production db is not compatible.

Question: how can I update my DbContext to reflect these changes?

By default update-database does not update the right database, since I have two databases.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Your question is really confusing. DataContext is LINQ to Entities, not Entity Framework. Do you mean DbContext? What do you want to do, update your entity classes to represent the old database, or update your old database to include the properties of the newer model? Is this code first or model first? In case of the latter, just right-click your EDMX and select "Update Database from Model" or "Generate Database from Model". – CodeCaster Dec 01 '15 at 14:38
  • sorry about the messy question. Yes I mean DBContext. Code 1st.I want to update the old database to include the properties of the newer model. – Terry Mosoma Dec 01 '15 at 14:44
  • So your problem is getting `Update-Database` to work. If your problem is that you have two databases (which shouldn't be a problem), you can select which one to update [using the `-ConnectionStringName` option](http://stackoverflow.com/questions/9441530/code-first-migration-with-connection-strings). See also [MSDN: Code First Migrations with an existing database](https://msdn.microsoft.com/en-us/data/dn579398.aspx). – CodeCaster Dec 01 '15 at 14:45
  • Hey Thanks for the tutorial, it was very helpful. I ran Update-Database -Force (and -Verbose) now I get "There is already an object named 'Test' in the database", there are other tables that should be updated. any idea how I can force the migration to take place? – Terry Mosoma Dec 01 '15 at 15:20
  • I think you need to add a (temporary) migration with the `-IgnoreChanges` flag, as explained in the MSDN link I gave. After updating the database to that migration, you can remove that migration and migrate to the newest version. But I can't verify that, you'll have to experiment a bit. You can always start with an empty database, of course. – CodeCaster Dec 01 '15 at 15:39
  • Depending on complexity, you could also look over the Up() code and comment out any changes that are already in the database before updating. – Steve Greene Dec 01 '15 at 15:42
  • @CodeCaster `-IgnoreChanges` and selecting the correct connection string worked well :) Much appreciated! – Terry Mosoma Dec 02 '15 at 09:41

0 Answers0