Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Automatic Migrations Existing Database

I am building an ASP.Net MVC4 web application with Entity Framework 5. I had to use an existing sql server database but also wanted to use Code First, so I followed this tutorial http://msdn.microsoft.com/en-us/data/jj200620.aspx

My application uses automatic migrations with Entity Framework.

The version of sql server I was using throughout the development phase was 2008, however, at the last minute I've been told the database needs to work on sql server 2005. I got the database I was using in sql server 2008 setup (exact tables, property names, relationships etc) on sql server 2005. However, with my web config etc setup to point to the sql server 2005 database, when I make a change to my one of my model classes, the automatic migrations don't work.

I get the following error

There is already an object named 'tblcourseprofessionTarget' in the database.

Can anyone please help?

Thanks.

Apologies if I haven't added enough information.

like image 216
tcode Avatar asked Dec 16 '22 13:12

tcode


2 Answers

Folks

The answer to my problem above was helped by this article http://christesene.com/entity-framework-4-3-code-first-with-automatic-migrations/

I had to delete my previous initial migration class, and re-create it.

like image 193
tcode Avatar answered Jan 10 '23 16:01

tcode


I had a similar problem.

The cause for me was that the same migration had been run already in the database but under a different MigrationID. I had 201504302005411_InitialCreate but my migration script was called 201505041911076_InitialCreate.

I fixed the issue by renaming the MigrationID in the database to match the one in my code.

I don't quite know how I ended up in the situation but renaming the MigrationID meant the app knew the script had already been run.

like image 30
Mr. Flibble Avatar answered Jan 10 '23 15:01

Mr. Flibble