1

When I run my application it says the model backing my context has changed since the database was created.

I added a field to my model like:

public int? LocationId {get;set;}

I added an int column to the database, and it is nullable.

I understand the database has that _migrationsHistory table that is now not in sync.

What I have been doing so far in QA is just dropping all the tables and then running the application which just re-creates the database automatically for me.

Now I want to keep the data in the table, how can I fix this issue?

I added the column so it should work, but when it performs the validation during startup if fails.

What options do I have?

Electric Coffee
  • 11,733
  • 9
  • 70
  • 131
public static
  • 12,702
  • 26
  • 66
  • 86

1 Answers1

1

It sounds like you want to use Code-First Migrations (EF 4.3 and later)

This would allow you to generate migration files based on changes to the model via the Package Manager Console.

you can generate a migration file by using add-migration <name>, make changes to it if required and use update-database to manually update.

You can also roll-back to any migration point if you inadvertently break something.

Sirhc
  • 518
  • 7
  • 13
  • Thanks I did that, but in production I cannot connect to the database from my laptop so will it run the migration during application startup? – public static Oct 09 '14 at 14:00
  • Not by default, but i think this will help you http://stackoverflow.com/questions/10848746/using-entity-framework-code-first-migrations-in-production – Sirhc Oct 09 '14 at 15:35