'Why do my EF tests on localdb work on "run" but not "debug"?

Today I found I was no longer able to debug tests involving EF Core. Each such test runs using LocalDB, and starts with a call to a function that calls this code:

db.Database.EnsureDeleted();
db.Database.EnsureCreated();

And before I continue, here's my connection string: @"Server=(localdb)\MSSQLLocalDB;Integrated Security=true;Database=TESTING"

This has always served its purpose of preparing a fresh database called TESTING for each test. But now, I get the following error: Cannot open database "TESTING" requested by the login. The login failed. Login failed for user 'xx\yy'.

Okay. That brought me to Cannot connect to (LocalDB)\MSSQLLocalDB -> Login failed for user 'User-PC\User', which I've actually referred to when solving a different issue in the past. But this time, no changes.

I tried pushing my code and running the tests in Azure Pipelines. It worked. I tried locally running all tests. It also worked. I tried running a single, individual test by using the "run" button instead of the "debug" button in Visual Studio. Perplexingly, that also worked. I rolled back to a commit for a previous version that I know was working at one point, and the behavior is the same as it is now.

So... any ideas?? Seems like it's unrelated to my code and may be either a Visual Studio or LocalDB issue but I'm not sure where to start with either of those.

LocalDB 2019 and Visual Studio 2022 by the way.



Solution 1:[1]

I ended up repairing my VS installation (which I believe includes reinstalling LocalDB with the data workload?) and everything seems to be working properly now. Still not sure what happened, but I'm glad it's working.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 monadoboy007