4

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.

tcode
  • 5,055
  • 19
  • 65
  • 124
  • Did you created the 2005 database manually or using the migrate.exe (or with the Update-Database command)? If you did it manually, did you copied to the __MigrationHistory table (normally it is a system table)? – Leo Mar 10 '13 at 13:44

3 Answers3

8

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.

tcode
  • 5,055
  • 19
  • 65
  • 124
  • Using ef7 3 years on and I had the same issue :)! As the article says, removing everything from my initial `Up` and `Down` migration fixes the issue for *all* future migrations. – James Apr 22 '16 at 11:32
4

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.

Mr. Flibble
  • 26,564
  • 23
  • 69
  • 100
  • It looks like I am experiencing the same issue. Since I haven't deployed the first version yet, is there any advice you could give me? I am wondering whether it does make sense to enable migration before deploying the app, so the MigrationId table and the corresponding InitialMigration class have the same ID? Any cons? – peval27 Oct 10 '16 at 07:48
  • I know this is a bit old, but I'm facing the same issue. I had the migrationId `201708250106326_InitialCreate` successfully created. But I don't know how, it was renamed to `201802011731015_InitialCreate` after trying to execute other migrations. And the worse, the framework was trying to re-execute all the migrations. Did you managed to find the cause of your issue? – jvretamero Feb 21 '18 at 00:24
0

if your context is mine: added a new entity class worked on it but, when I tried to migrate it shows this error, try removing that object from db context then run update-database and add that object again and rerun update-database

for example:

public DbSet<CustomizedEmail> CustomizedEmail { get; set; }
public DbSet<KeyWordsForEmail> KeyWordsForEmail { get; set; }
public DbSet<Notice> Notice { get; set; }//remove it>run update-database>
//add again the above entity and rerurn update-database

this works whether you have data or not. in this process you will loose data in that table.