English 中文(简体)
实体框架给出异常:“基础提供程序在打开时失败。”
原标题:Entity Framework giving exception : "The underlying provider failed on Open."

我有一个测试。发生的情况是,无论何时首先执行test1,test2都会失败,并显示以下消息:

"System.Data.EntityException : System.Data.EntityException : the underlying provider failed on open. cannot open database "DBEntities" requested by the login. The login failed.".

无论何时首先执行test2,test1都会以相同的消息失败。

在过去的三天里,我一直在做这件事。

[TestClass]
class MyTestClass 
{

    DBEntities db;

    [TestInitialize]
    public void Initializer()
    {
       db = new DBEntities(); 
    }

    [TestMethod]
    public void test1()
    {
       db.Books.Add(new Book{ ...});
       db.SaveChanges();

    }

    [TestMethod]
    public void test2()
    {
       db.Books.Add(new Book{ ...});
       db.SaveChanges();
    }


    [TestCleanup]
    public void Clean()
    {
       db.Dispose(); 
    }

}

以下是DBEntities类的摘录:

public class DBEntities : DbContext
{
    public DbSet<Books> TheBooks{get;set;}
    ...
}

运行探查器后,我注意到以下消息:

“用户登录失败。原因:未能打开明确指定的数据库。”

在大量lockAcquiredlock Release语句之后。

问题回答

问题可能是连接池?SQL提供程序是否会尝试保留测试之间的连接,即使EF对象已被释放?在这种情况下,是否会出现同一用户多次连接到同一数据库的问题?

尝试在清理中显式关闭连接:

db.Database.Connection.Close();

信息的重要部分是:

用户domain1user1登录失败。。

如果数据库拒绝您传递的登录信息,则EF无法工作。

有时,你可以在运行框中键入“iisreset”,它会清除你描述的问题,特别是如果一切正常,突然停止工作。发生此错误的原因可能是系统内存不足。(至少就我而言)

如果您正在通过IIS运行,请检查附加到web应用程序的IIS池的标识





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签