简体   繁体   English

实体框架4.3不创建数据库

[英]Entity Framework 4.3 doesn't create database

I created new project and added the newest entity framework to it (version 4.3). 我创建了新项目并添加了最新的实体框架(版本4.3)。 I created classes and the context as in previous EF versions. 我像以前的EF版本一样创建了类和上下文。 However, during the very first run when the database should be created (in my case it is SQL Server 2005), I'm receiving the following error: 但是,在第一次运行时应该创建数据库(在我的情况下是SQL Server 2005),我收到以下错误:

An error occurred while executing the command definition. 执行命令定义时发生错误。 See the inner exception for details. 有关详细信息,请参阅内部异常

With the following inner exception: 具有以下内部异常:

Invalid object name 'dbo.__MigrationHistory'. 无效的对象名称'dbo .__ MigrationHistory'。

As I understand, this table is for migrations, but this table does not exist if there is no database. 据我了解,此表适用于迁移,但如果没有数据库,则此表不存在。 Am I doing something wrong? 难道我做错了什么?

More info: For testing purposes I created only one class: 更多信息:出于测试目的,我只创建了一个类:

public class Test
{ 
  [Key]
  public int TestId { get; set;}

  public string Name {get; set;}
}

public class Context : DbContext
{
   public Context() : base("MyConnection")
   {
   }

   public DbSet<Test> Tests { get; set;}
}

UPDATE 1 更新1

After some tests I realized that application is throwing unhandled exception from visual studio and break in visual studio. 经过一些测试后,我意识到应用程序正在从视觉工作室抛出未处理的异常并在视觉工作室中中断。 The exception was System.Data.EntityCommandExecutionException . System.Data.EntityCommandExecutionException是一个例外。 Once I ignored that expection and didn't stop code execution, database was created. 一旦我忽略了这个期望并且没有停止代码执行,就创建了数据库。

UPDATE 2 更新2

After another few hours working with database I found out that playing with Enable-Migrations option and Update-Database from console also is solving that issue. 在使用数据库几个小时之后,我发现使用Enable-Migrations选项和从控制台更新数据库也正在解决这个问题。 It is creating database before application start and don't break in Visual Studio. 它在应用程序启动之前创建数据库,并且不会在Visual Studio中中断。

Could you try removing your constructor to make EF use it's default connection string. 您可以尝试删除构造函数以使EF使用它的默认连接字符串。

public Context() : base("MyConnection")
{
}

Failing that, could you try updating your database from the Package Manager Console to see if you get any further information. 如果不这样做,您是否可以尝试从软件包管理器控制台更新数据库,看看是否有任何进一步的信息。

Update-Database -Verbose

Possibly unrelated in your case, but I get the same error when using MvcMiniProfiler 1.9. 在您的情况下可能不相关,但在使用MvcMiniProfiler 1.9时我得到相同的错误。 If you are using it too, make sure EF profiling is turned off by commenting out the line: 如果您也在使用它,请通过注释掉该行来确保关闭EF配置文件:

//MiniProfilerEF.Initialize();

Within the MiniProfiler App_Start. 在MiniProfiler App_Start中。


For others experiencing a similar issue, I have found that reenabling migrations from the Package Manager Console can help in certain cases. 对于遇到类似问题的其他人,我发现在某些情况下从Package Manager控制台重新启用迁移可能会有所帮助。 Make sure you have a copy of your Migration configuration before doing this. 在执行此操作之前,请确保您拥有迁移配置的副本。

Enable-Migrations -Force

...and just to add one more possible answer for all those facing similar problem ......并且只为所有面临类似问题的人添加一个可能的答案
(note: this is an open-ended story seems, as there're obviously some bugs still with the migration part)... This link came closest to what I needed (注意:这是一个开放式的故事似乎,因为迁移部分显然还有一些错误)...这个链接最接近我需要的
Error when running Update-Database with EF 4.3 so, you need to do 3 things (in that order - and I'm referring to an existing project): 使用EF 4.3运行Update-Database时出错,因此,您需要执行3项操作(按此顺序 - 我指的是现有项目):
(all is in PM console) (都在PM控制台)

  • Make sure that 'default project' in PM Console is set to your desired project (ie for larger solutions) - that doesn't necessarily match your startup project! 确保PM控制台中的“默认项目”设置为您想要的项目(即对于更大的解决方案) - 这不一定与您的启动项目相匹配! (and closely watch the comments/response in PM as to whether the actions were made on the project you want) (并仔细观察PM中关于是否对您想要的项目进行了操作的评论/回复)
  • (1) Enable-Migrations -force (1) Enable-Migrations -force
  • (2) Add-Migration Initial (2) Add-Migration Initial
  • (3) Update-Database -Verbose (3) Update-Database -Verbose

...if you still get an exception in PM console ...如果您在PM控制台中仍然出现异常

  • (4) then you might need to 'move' your project into the root (4) 然后你可能需要将你的项目“移动”到根目录中

it sounds silly I know, but that was the main problem on my side - I had a bunch of solution folders and any of the above would fail on projects within solution folders. 这听起来很傻我知道,但那是我身上的主要问题 - 我有一堆解决方案文件夹,上面的任何一个都会在解决方案文件夹中的项目上失败。 However, once I moved the project to the root, everything worked fine (no more exceptions, with or w/o first-chance exceptions for CLR turned on or off)... 但是,一旦我将项目移动到根目录,一切正常(没有更多例外,有或没有CLR开启或关闭的第一次机会异常)...
hope this helps somebody 希望这有助于某人

EDIT: if your data model project (EF CF) is a library - then set that project as a 'default project' in PM console - and run all those things above on that project directly (and have migration configuration etc. created in the lib itself). 编辑:如果您的数据模型项目(EF CF)是一个库 - 然后将该项目设置为PM控制台中的“默认项目” - 并直接在该项目上运行上述所有内容(并在lib中创建迁移配置等)本身)。 Otherwise it'd fail (and the same no MigrationHistory exception also appears when your model is a lib - and no migration defined for it, within it - and you have migration defined on the 'main project'). 否则它会失败(并且当你的模型是一个lib时,同样没有出现MigrationHistory异常 - 并且没有为它定义迁移,并且你在'主项目'上定义了迁移)。

EDIT: you'd need to move both the lib (EF model) and the 'startup' project (calling it) into the root. 编辑:你需要将lib(EF模型)和'startup'项目(调用它)移动到根目录中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM