0

I am using Entity Framework 6.3 for an MVC 5 application, using code-first (for the first time) with SQL Server 2012. The Database does not exist before hand, it is all being created from scratch by EF. When I attempt to start the application, and hence EF should create the database, it's giving me the following SqlException error message:

"There is already an object named 'TableName' in the database."

I have in my project the following very basic Entity:

[Table("TableName")]
public class ItemStatus
{
    public int id { get; set; }
    public string Status { get; set; }
}

That's all it is.

If I change the name of TableName to something else, like foofoo, then the error message reflects this change. So I've not put two Models in with the same table name or anything obvious like that.

Everything had been working fine until just recently, I'm not sure what I could have changed to make it break like this. The last thing I did before it stopped working was add a new View Model. Removing this completely doesn't help.

Additional information 1: My solution has two projects, each with a DbContext. DbContext2 from Project2 inherits from DbContext1 from Project1 - which in turn is inheriting from IdentityDbContext. I was thinking that maybe this has something to do with it? Like both projects are trying to Create the same table for some reason. Although I'm pretty sure it should check if the table exists or not before trying to create it.

Additional Information 2: When I look at the Database server after getting this error, I see that it has created multiple other tables from entities in Project1 with no apparent problems.

Additional Information 3: If I hadn't made it clear yet, it was working fine earlier today. I've had that Entity in my project for a while with no problems, and I'd not changed anything about it before getting this error.

Thanks in advance for any advice.

Edit: I should have mentioned that I'm using MigrateToLatestVersion as my initialization. I don't get the problem if I use DropCreateDatabseAlways.

Dean
  • 85
  • 8
  • Prior to this not working, did both projects create their respective DB objects in the same DB schema? – Phil Walton Apr 24 '15 at 15:13
  • Yes they do. Well, Project2 creates a database with DbContext2. DbContext2 inherits from DbContext1. Project1 can't run by itself. – Dean Apr 27 '15 at 12:16
  • Is your code using that particular table `[Table("TableName")]` elsewhere? – Phil Walton Apr 27 '15 at 13:11
  • No. I can be sure of this because if I change the name of this particular table to one I'm definitely not using (foofoobaahbaah, for example), then I get the same error, except this time it says it's trying to make object foofoobaahbaah and can't because it already exists. – Dean Apr 28 '15 at 12:16
  • This article helped. http://stackoverflow.com/questions/11679385/reset-entity-framework-migrations. Reset the framework migrations. – Ryan_D Apr 28 '15 at 13:04
  • Possible duplicate of [Database already exist. Choose a Different Name using CreateDatabase()](http://stackoverflow.com/questions/7775409/database-already-exist-choose-a-different-name-using-createdatabase) – Michael Freidgeim Apr 21 '17 at 08:06

2 Answers2

0

Are you using automatic migrations? Are you using MigrateDatabaaseToLatestVersion initializer? Maybe _Migrations table got damaged somehow? I believe EF thinks that your initial migration needs to run again, which you can confirm by running SQL profiler. Maybe something got renamed incorrectly?

Sergey Barskiy
  • 1,761
  • 2
  • 15
  • 17
  • I was using MigrateDatabaseToLatestVersion. If I change to the DropCreateDatabaseAlways initialization option the problem, perhaps unsurprisingly, goes away. Changing back to MigrateDatabaseToLatestVersion, after running the DropCreateAlways option once, sees the problem resume. There is a migration history table but it doesn't have any useful looking information in it. – Dean Apr 28 '15 at 12:10
-1

I had this issue before.

Go to SQL server installation directory and try to remove *.ldf and *.mdf files. My files are located at:

C:\Program Files\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL\DATA\

You may need to stop SQL server before. If it doesn't work, try to find *.ldf and *.mdf files in your solution directory or in C:\Users\YOUR_USER_NAME\

If it doesn't work, try to create another database. It is also easy to miss database name configuration in connection strings.

If you have multiple projects, try to check connection strings in all of *.config files.

Roman Pushkin
  • 5,639
  • 3
  • 40
  • 58