实体框架4.3不创建数据库 [英] Entity Framework 4.3 doesn't create database

查看:139
本文介绍了实体框架4.3不创建数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创造了新的项目,并增加了最新的实体框架,它(版本4.3)。我创建的类和上下文在previous EF版本。但是,应该创建数据库时的第一运行期间(在我的情况下,它是SQL Server 2005中),我收到以下错误:

I created new project and added the newest entity framework to it (version 4.3). I created classes and the context as in previous EF versions. 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:

在执行命令定义时出错。详情请参阅内部异常。

An error occurred while executing the command definition. See the inner exception for details.

使用以下内部异常:

无效的对象名称DBO .__ MigrationHistory。

Invalid object name '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;}
}

更新1

一些测试后,我意识到,应用程序从Visual Studio抛出未处理的异常,并在Visual Studio中打破。唯一的例外是的 System.Data.EntityCommandExecutionException 的。有一次,我忽略了厚望并没有停止code执行,创建数据库。

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. Once I ignored that expection and didn't stop code execution, database was created.

更新2

使用数据库的另一个几个小时后,我发现,与启用,迁移选项,并更新数据库内的控制台还是解决这一问题的播放。它创建应用程序开始之前的数据库,也不要在Visual Studio中打破。

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. It is creating database before application start and don't break in Visual Studio.

推荐答案

你能不能尝试删除你的构造​​函数,使EF使用它的默认连接字符串。

Could you try removing your constructor to make EF use it's default connection string.

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


在你的情况可能不相关,但在使用MvcMiniProfiler 1.9我得到同样的错误。如果你正在使用它太,确保EF分析被注释掉该行关闭:


Possibly unrelated in your case, but I get the same error when using MvcMiniProfiler 1.9. If you are using it too, make sure EF profiling is turned off by commenting out the line:

//MiniProfilerEF.Initialize();

在MiniProfiler App_Start。

Within the 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. Make sure you have a copy of your Migration configuration before doing this.

Enable-Migrations -Force

这篇关于实体框架4.3不创建数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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