Entity Framework code first migration giving SqlException: “There is already an object named 'TableName' in the database.”

会有一股神秘感。 提交于 2019-12-12 02:32:33

问题


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.


回答1:


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?




回答2:


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.



来源:https://stackoverflow.com/questions/29851106/entity-framework-code-first-migration-giving-sqlexception-there-is-already-an

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!