Troubleshooting Database Connectivity Issues When Unit Testing With MSTest
I was attempting to run some unit tests today for an ASP.NET MVC project I'm working on when I ran into the following fun little exception: Test method BlogMVC.Tests.Controllers.HomeControllerTest.Index threw exception: System.Data.EntityException: The underlying provider failed on Open. ---> System.Data.SqlClient.SqlException: An attempt to attach an auto-named database for file C:\Users\Alan\Documents\Visual Studio 2010\Projects\BlogMVC\TestResults\Alan_QUIGON 2012-05-15 16_54_57\Out\BlogMVC.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share. The issue is that each unit test is run in it's own individual directory, and in my case the unit tests needed my database files, which were not being deployed to the unit test directory. Fortunately this is easy to correct. To fix this issue, do the following: Right-click on the solution (not the project) and select "Add -> New Item". Choose "