The process cannot access the file '.mdf' because it is being used by another process

asp.net-mvc-3, c#, sql-server-2008, sql-server-2008-r2

Solution

If you have accessed this mdf file before using either Sql Management Studio or your Visual Studio xx. Then please close Sql Server running this mdf file, I mean, you have to stop the Sql Server Services and then copy the mdf file from its location to the desired location.

After copying, you can very well restart the Sql Server.

Problem

I use the ASP.NET Dynamic Data web template with an SQL Server 2008 database and when I try to add `.mdf` file in `App_Data` I get The file cannot be opened because it is being used by another process. Please close all applications that might access this file and try again I try find solutions but its very different Hi Ken, this is because you are trying to add your DB to the project (i.e. copy it there) not connect to it, to connect add a Entity Framework data model and then select your Data base and all should go well. If you actually want to have the DB in the App_Data folder then you will need: To have SQL Server Express installed locally and on your web server. Temporarily stop your SQL Server so that the DB is accessible then copy it to the App_Data folder. Or Go to SQL Server Configuration Tools and stop the SqlserverExpress Service. In Solution Explorer a Right click on project add existing Item ., Go to Program files and search for you required `.mdf` file in it add it to Project. Goto SQL Server Configuration tools start the Sqlexpress Service. Run Your solution. How I can resolve my issue?

Original source