35632

Entity Framework code first migration giving SqlException: “There is already an object named 'T

Question:

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 <em>should</em> 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.

Answer1:

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?

Answer2:

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.

Recommend

  • Can't connect Entity Framework to local SQL Server Express
  • Sympy: working with equalities manually
  • How to populate html table with info from list in django
  • Efficient User-Agent Regex to find Safari in Python
  • Prevent page break in text block with iText, XMLWorker
  • Is it possible to get the word under the mouse cursor in a ``?
  • Shouldn't else be indented in the below code
  • Django model for a Postgres view
  • Combining two different ActiveRecord collections into one
  • WPF - CanExecute dosn't fire when raising Commands from a UserControl
  • Java: can you cast Class into a specific interface?
  • Spring Data JPA custom method causing PropertyReferenceException
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • Jenkins: How To Build multiple projects from a TFS repository?
  • Why ng-show works with ng-repeat but ng-if doesn't? [duplicate]
  • Sony Xperia Z Tablet not found by adb
  • AES padding and writing the ciphertext to a disk file
  • How to recover from a Spring Social ExpiredAuthorizationException
  • Excel - Autoshape get it's name from cell (value)
  • Updating server-side rendering client-side
  • How to extract text from Word files using C#?
  • How to apply VCL Styles to DLL-based forms in Inno Setup?
  • How can I estimate amount of memory left with calling System.gc()?
  • Delete MySQLi record without showing the id in the URL
  • AT Commands to Send SMS not working in Windows 8.1
  • Unanticipated behavior
  • Comma separated Values
  • Matrix multiplication with MKL
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • KeystoneJS: Relationships in Admin UI not updating
  • How to set the response of a form post action to a iframe source?
  • Hits per day in Google Big Query
  • Trying to get generic when generic is not available
  • Change div Background jquery
  • File not found error Google Drive API
  • Qt: Run a script BEFORE make
  • apache spark aggregate function using min value
  • Sorting a 2D array using the second column C++
  • reshape alternating columns in less time and using less memory
  • Converting MP3 duration time