1

I have an EF code first model generated by reverse engineering an existing database - one of the supported core scenarios for EF 6.

I now have updates to the DB and I want to reflect those in the model, but I simply cannot find a mechanism to update the generated model. In the "old" EDMX world, I could update model from database, but I cannot see how to do this in VS 2013 with EF 6?

I have tried to run a migration against the new database but no changes were made to the POCOs.

Matt
  • 25,467
  • 18
  • 120
  • 187
Colin Desmond
  • 4,824
  • 4
  • 46
  • 67
  • Wouldn't "code first" imply that the model should be changed and the database would be updated as a result? If the database was changed directly then it sounds like you'd either need to re-generate your models or update your models directly. (I normally do the latter.) – David Sep 15 '14 at 13:13
  • 1
    afaik this is normal. From another point in "code first", you have **first**. That is first the code, then the migration and finally the db is updated. Otherwise you are in "code first on existing database", and again afaik, what you need is not currently supported. – tschmit007 Sep 15 '14 at 13:13
  • To avoid that you're mixing up things, I recommend you read this: https://stackoverflow.com/questions/5446316/code-first-vs-model-database-first – Matt May 28 '19 at 15:47

1 Answers1

0

To update your database in code first projects, do the following:

  1. Find out the name of the database context by checking your source code, in the following steps I assume it is ConfigDbContext
  2. Open PM console via menu Tools -> NUGET Package Manager -> Package Manager Console
  3. Type the following:
    PM> add-migration nameofmigration -context ConfigDbContext
    PM> update-database -context ConfigDbContext
    Note: Replace nameofmigration by any unique name of your choice so you easily remember the changes. The migration code will be named with a timestamp and this name.
  4. If you got a success message, open SQL Management Studio, connect to your database, or, if you have it already open, refresh and review the changes.

Note: If you're getting a message that you should update EF core tools, here is how you can do that.
For example:
PM> Install-Package Microsoft.EntityFrameworkCore.Tools -Version 2.2.0
Replace the version number 2.2.0 by the newer version you get displayed in the warning.

Matt
  • 25,467
  • 18
  • 120
  • 187