10

I have a test. What happens is that whenever test1 is executed first, test2 fails with the message:

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

and whenever test2 is executed first, test1 fails with the same message.

I've been at this for the past 3 days.

[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(); 
    }

}

Here's an excerpt of the DBEntities class :

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

After running a profiler, I noticed this message:

"Login failed for user. Reason: failed to open the explicitly specified database."

after a lot of lockAcquired and lockReleased statements.

yoozer8
  • 7,361
  • 7
  • 58
  • 93
Attilah
  • 17,632
  • 38
  • 139
  • 202
  • 4
    Just pasting the exception message isn't going to help the community help you. What's your connection string? Has the database db1 been opened? What version of SQL are you using? You need to do some work before anybody else will – heads5150 Feb 14 '11 at 00:10
  • the database db1 can be opened by the first test but the second test and third one can't get it opened. – Attilah Feb 14 '11 at 01:54
  • I see only two tests so which one is passing? – Ladislav Mrnka Feb 14 '11 at 12:58
  • whichever one runs first is the one that passes. – Attilah Feb 14 '11 at 14:22
  • This is very odd behavior; please use Profiler to see what's going on in the database (as is described in your dupe). Also, please don't ask the same question again. –  Feb 15 '11 at 16:25

4 Answers4

11

Might the issue be connection pooling? Would the SQL provider try to preserve the connection between tests, even though the EF object is disposed? In that case would it be having an issue w/ multiple connections to the same DB by the same user?

Try explicitly closing the connection in your cleanup:

db.Database.Connection.Close();
Justin Denton
  • 581
  • 3
  • 5
  • Hi Justin, I think you're onto something. because I've watched the profiler and there are lots of locks acquired and released from the same user and then suddenly the error : "Login failed for user. Reason : failed to open the explicitly specified database." – Attilah Feb 15 '11 at 17:13
  • 2
    'TestInitialize' & 'TestCleanup' are going to run before/after each test, so you're throwing a lot of traffic at the db provider to create/open/close connections. Particularly with the simple tests you show here, it seems likely that you're setting yourself up for race conditions (concurrency problems with your test code getting ahead of the provider). – Justin Denton Feb 15 '11 at 17:20
  • 2
    Would it be possible for you to create the db object once in a ClassInitialize method, rather than for each test? – Justin Denton Feb 15 '11 at 17:21
  • @Justin, I'm trying that right now and will let you know in a while – Attilah Feb 15 '11 at 17:27
  • @Justin, it works now that I've created a [ClassCleanup] and a [ClassInitialize] method. Thanks. Can you explain what was wrong ? more traffic than SQL Server Express could handle ? – Attilah Feb 15 '11 at 17:34
  • 2
    My *gut* feeling (ie, don't know for certain), is yes, the tests are requesting connections _faster_ than the provider can handle. It's not necessarily more traffic, but if your test code is simple it will execute quickly; managing db connections will call outside your code (eventually, maybe unmanaged) & probably be much slower. When you run one test, close the connection, then immediately run another test, the first connection is probably still open while you're requesting the next (for the same user to the same db file) -> conflict. – Justin Denton Feb 15 '11 at 17:50
4

The important part of the message is:

Login failed for user 'domain1\user1'..

If the DB rejects the login info you pass, then the EF can't work.

Craig Stuntz
  • 125,891
  • 12
  • 252
  • 273
  • 1
    Actually, it accepts the login info from the first test (whichever one runs first is the one that passes.) and it is denied from the rest even though they all use the same login info. – Attilah Feb 14 '11 at 14:24
  • I understand that the first test works and the second doesn't, but the message is unambiguous. The DB server is denying the login. Therefore, your assertion that they're using the "same login info" *cannot* be correct. Something has changed for the second test. – Craig Stuntz Feb 14 '11 at 14:29
  • I'm using built in Windows Authentication in the config file. I didn't specify any password. so, I don't see why the failure occurs. or maybe SQL Server Express doesn't allow multiple connections from the same client ? – Attilah Feb 14 '11 at 17:08
1

Sometimes, you can type "iisreset" in the run box it will clear up the issue you described, especially if everything was working fine and all of the sudden it stopped working. This error can happen due to lack of memory on your system. (In my case at least)

Start >> Run >> iisreset

Gaff
  • 5,507
  • 3
  • 38
  • 49
  • Thanks for the "lack of memory" tip - I fixed the database and found a memory leak with this. – Scott Apr 02 '15 at 19:07
0

If you are running through IIS, please check identity of IIS pool attached to web application

Mian
  • 231
  • 5
  • 7