Creating connection to MDF-file in Visual Studio: "File is in use"

Aske B. picture Aske B. · Sep 7, 2012 · Viewed 14.9k times · Source

Introduction

I'm new to the Entity Framework and setting up a correct SQL Server.

I'm following the official Entity Framework tutorial and I'm at the "Creating the Database"-step about 30% down the page.

Steps to reproduce the problem

copy the School.mdf file from the following folder to your application's App_Data folder

Instead of using the School.mdf file I'm using my own file that was automatically created by using SQL Server Management Studio (I downloaded that from the Microsoft Download Center, selecting the ENU\x86\SQLEXPRADV_x86_ENU.exe option) to create a database with tables.

When in Visual studio 2010 Ultimate, I:

  1. Go the Server Explorer and right click on Data Connections --> Add Connection:

  2. Choose the Microsoft SQL Server Database File-option for Data source and press Continue.

  3. Click on Browse... under Database file name.

  4. Select the file in the %PROGRAMFILES%\Microsoft SQL Server\MSSQL11.MOBILIZEMESQL\MSSQL\DATA directory, since that's where it's saved.

  5. Get the error.

Picture version:

1.

Add database connection step 1

2.

Add database connection step 2

3.

Add database connection step 3

The problem

I get the error message:

DateBaseName.mdf
This file is in use.
Enter a new name or close the file that's open in another program.

What I've tried

Since the tutorial doesn't tell me directly how to do this I first figured my goal was to find a way to open .mdf-files, and this was the one I could find.

I'm thinking I'm getting the error because the SQL Server is running (since the Management Studio isn't), since I don't know what else should be using the file.

I could copy the file into another directoy, but then wouldn't I have to update the file everytime I change the database?

Since I'm new to this, I know I'm missing something obvious here.

The question

Is there a proper way of connecting a database to Entity Framework?

Answer