实体框架code首次迁移给予的SQLException:"还有已经在数据库中名为'表名'的对象"。 [英] Entity Framework code first migration giving SqlException: "There is already an object named 'TableName' in the database."

查看:183
本文介绍了实体框架code首次迁移给予的SQLException:"还有已经在数据库中名为'表名'的对象"。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用实体框架6.3的MVC 5应用程序,使用code-第一(首次)与SQL Server 2012数据库不前手存在,它是所有正在从从头开始创建EF 。当我尝试启动应用程序,因此EF应该创建数据库,这是给我的SQLException以下错误信息:

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; }
}

这就是它。

如果我更改表名的名称到别的东西,像foofoo,那么错误信息反映了这种变化。所以我没有把两种型号使用相同的表名或任何明显的那样。

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.

其他信息1:
我的解决方案有两个项目,每一个的DbContext。 DbContext2从Project2中从DbContext1继承PROJECT1 - 这又是从IdentityDbContext继承。我在想,也许这有某种关系呢?这两个项目就像试图出于某种原因,创建相同的表。虽然我pretty确保它的的检查表试图创建它之前存在与否。

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.

其他信息2:当我看着收到此错误后,数据库服务器,我看到它已经创建了多个其他表从实体PROJECT1没有明显的问题。

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.

其他信息3:如果我还没有作出明确的是,这是今天早些时候工作的罚款。我有这样的实体在我的项目没有问题了一段时间,收到此错误之前,我不会改变任何事情。

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.

编辑:我应该提到,我使用MigrateToLatestVersion作为我的初始化。如果我使用DropCreateDatabseAlways我不明白这个问题。

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

推荐答案

您使用自动迁移?您是否使用MigrateDatabaaseToLatestVersion初始化?也许_Migrations表得到了不知何故损坏?我相信EF认为您最初的迁移需要重新运行,您可以通过运行SQL事件探查确认。也许得到的东西不正确地改名?

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?

这篇关于实体框架code首次迁移给予的SQLException:"还有已经在数据库中名为'表名'的对象"。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆