2

We have an application that was created with Entity Framework 6.0 and uses Code First to create our database schema. The DBA has created a new column in production on an already existing table and wants us to map to it.

Looking at Entity Framework 4.1 code first mapping to already existing database table I see the mention of using Ignore, but we want the field to be mapped; we just don't want to create it.

Worst case, we'll have the DBA copy the columns values to a new temp column, create it with code first in the traditional way, then copy it back. We'd rather have code first deal with it if possible, though. Ideally it would create the column in dev/test/uat environments if it doesn't exist, but not delete it in production.

Community
  • 1
  • 1
Kirk Liemohn
  • 7,733
  • 9
  • 46
  • 57

2 Answers2

4

Based on the comments above, I know what you can do to solve this:

  1. Make sure that your POCO has the column that was added to the table

  2. Go to NuGet's Package Manager Console and run add-migration. This will generate a manual migration file which will include some of the changes to the database that it detects needs to be there.

  3. Delete everything inside both the Up and Down methods. Just leave the signatures and an empty method for both of them.

  4. Finally, run your app with the MigrateDatabaseToLatestVersion initializer on. You shouldn't see any errors after that.

I've had the same problem happen to me before, and this has always solved it. I don't know why Microsoft has it working this way, but that's just the way the cookie crumbles I guess.

Corey Adler
  • 15,897
  • 18
  • 66
  • 80
1

I run into the same issue - after add-migration on an empty database (or not yet created DB), EF notified me that there are some changes in model so I should run add-migration.

Unable to update database to match the current model because there are pending changes and automatic migration is disabled.

When I run "add-migration", new migration file just adds some ALREADY EXISTING columns, so next Update-Database failed on error that I'm trying to add column, that already exists.

So what helped to me:

  1. Run add-migration
  2. In new generated file remove content of Up and Down methods (or just rows that causes the issue)
abatishchev
  • 98,240
  • 88
  • 296
  • 433
Tomino
  • 5,969
  • 6
  • 38
  • 50