实体框架4.3.1未能创建(/打开)数据库[​​线程异常?] [英] Entity Framework 4.3.1 failing to create (/open) a database [Threading Anomaly?]

查看:349
本文介绍了实体框架4.3.1未能创建(/打开)数据库[​​线程异常?]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用EF 4.1(的Code First )在MVC 3项目而回,这是不错的。

I've used EF 4.1 (Code First) in an MVC 3 project a while back, and it was good.

今天我尝试使用EF 4.3.1(的Code First )在WinForms项目和遇到的一些真正的巫术(原始项目我工作的是的WinForms,但同样的事情是真实的的的控制台应用程序代码)

Today I tried using EF 4.3.1 (Code First) in a WinForms project and encountered some real voodoo: (The original project I was working on was WinForms, however the same thing is true for the attached Console Application code.)

当试图进入一个简单的类到数据库中,我得到了以下异常:
无法打开数据库测试登录所请求。登录失败。
用户登录失败'[计算机] \ [管理员]'。

When trying to enter a simple class into the database, I get the following exception: Cannot open database "Test" requested by the login. The login failed. Login failed for user '[ComputerName]\[Administrator]'.

我试过检查的SQL Server(2008 R2)配置,但我的用户的确实的在服务器上的所有权限

I've tried checking the SQL Server (2008 R2) configurations but my user does have all the permissions on the server.

注意:数据库< 。/ em>的不不可以存在,当我第一次运行该应用程序

NOTE: The Database does not exist when I first run the application.

即使下面的示例代码无法正常工作:

Even the following sample code does not work :

class Program
{
    public static void Main()
    {
        var person = new Person { Name = "Nathan" };
        using (var db = new MyContext())
        { //#1
            db.Persons.Add(person);
            db.SaveChanges();
        }
        Console.Write("Person saved !");
        Console.ReadLine();
    }
}

public class Person
{
    public int PersonId { get; set; }
    public string Name { get; set; }
}

public class MyContext : DbContext
{
    public DbSet<Person> Persons { get; set; }
    public MyContext()
        : base("Data Source=localhost;Database=Test;Integrated Security=True;")
    { }
}

我试着重新安装EF 4.3.1 - 无济于事。
重新启动SQL-Server还没有效果。

I've tried re-installing EF 4.3.1 - to no avail. Restarting the SQL-Server also had no effect.

注意:我已经注意到了相当多的类似的问题身边,但没有都像我的的回答是相同的 - 这里是我的希望,我会得到幸运在这里:)

NOTE: I've noticed quite a lot of similar questions around, but none are both the same as mine and answered - here's me hoping I'll get lucky here :)

澄清: 用户被拒绝访问机器的所有者(本地管理员)

To clarify: The user being denied access is the Owner (local administrator) of the machine.

编辑:我已经运行几个实验,我已经发现了一些很奇怪:

I've been running a few experiments and I've found something quite strange:

如果我暂停程序在该行标记为#1和检查价值db.Database .Exists()通过的手表的窗口,它告诉我:功能评估需要所有的线程来运行有那么一点的波浪卷发按钮,我可以点击,使其运行的所有线程。

If I pause the program at the line marked "#1" and check the value of db.Database.Exists() via the Watch window, It tells me: "The function evaluation requires all threads to run" with that little wavey button I can click to make it run all threads.

如果我点击该按钮的函数计算为

If I click on that button the function evaluates to False.

现在我可以做的 2 的事情:

Now I can do 2 things:


  1. 继续执行:在这种情况下,我得到完全相同的例外

  2. 在监视窗口执行db.Database.Create():在这种情况下,程序成功运行到结束,我看到了数据库,表和排在我的 SQL Management Studio中的创建。

  1. Continue execution: In this case I get the exact same exception.
  2. Execute db.Database.Create() from the Watch window: In this case the program runs successfully to the end and I see the Database, Table and Row created in my SQL Management Studio.

似乎是一个简单的解决方案,对不对? !错

Seems like a simple solution, right? Wrong!

我试过用前缀调用数据访问的 db.Database.CreateIfNotExists(); 但后来我得到的< 。STRONG>完全相同的例外在该行

I've tried prefixing the data access with a call to db.Database.CreateIfNotExists(); but then I get the exact same exception on that line.

db.Database.Initialize(真); 也无济于事..

注意:如果用上述方法我创建数据库,我可以正常开始使用它从该点,所以这至少一半慰藉:P问题是,当然,当我添加发生什么 DropCreateIfModelChanges DropCreateAlways Initalizer(因为我在模型的积极发展中间)。

NOTE: If by using the above method I create the database, I can use it properly from that point onwards, so that's at least half a consolation :P The problem is, of course, what happens when I add a DropCreateIfModelChanges or DropCreateAlways Initalizer (since I'm in the middle of active development of the model).

感谢。

[我怀疑它更是一个实体框架问题,不是的SQL Server。 ]

[I'm suspecting it's more of a Entity Framework problem than SQL Server.]

推荐答案

您可以看到,如果你碰巧有,当异常被抛出突破调试时,这个异常的选项(调试 - > Excptions)开启。 EF试图连接到数据库,但因为数据库中不存在却又如此抛出一个异常失败。你看例外,由于打开上面的选项。此异常实际上是由实体框架抓住,然后EF将连接到主数据库中创建它试图在第一步中连接数据库。一旦创建数据库,将重新使用原来的连接字符串交谈的数据库。所以,你既可以按F5继续调试,因为异常是将被处理或禁用第一次机会异常关闭,这将导致打破只适用于没有处理,而不是所有的异常异常时,抛出异常打破了抛出。

You can see this exception when debugging if you happen to have "break when exception is thrown" option (Debug->Excptions) turned on. EF is trying to connect to the database but it fails since database does not exist yet so an exception is thrown. You see the exception due to the option above turned on. This exception is actually caught by the Entity Framework and EF will then connect to master database to create the database it was trying to connect in the first step. Once the database is created it will again use the original connection string to talk to the database. So, you can either press F5 to continue debugging since the exception is a first chance exception that will be handled or disable turn off "breaking when exception is thrown" which will result in breaking only for exceptions that are not handled rather than all exception that are thrown.

这篇关于实体框架4.3.1未能创建(/打开)数据库[​​线程异常?]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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