9

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?

Aske B.
  • 6,419
  • 8
  • 35
  • 62
  • I'm not sure I really follow what you've done, however if the database is being hosted by SQL server express, why don't you connect via SQL server, rather than trying to open the db file? Use the 'Microsoft SQL server' data source and a server name of '.\SQLEXPRESS', then pick the database name from the drop down. That way you can manage the DB either from within VS, or the SQL management studio. – forsvarir Sep 10 '12 at 07:25
  • @forsvarir I'm a bit confused with the Compact Edition vs. Express vs. whatever else there is. According to [the site I downloaded it from](http://www.microsoft.com/en-us/download/details.aspx?id=29062), it seems to be `SQL Server® 2012 Express`. I tried picking the 'Microsoft SQL server' data source and then manually typing in '.\SQLEXPRESS' but only a few standard db's came up: `master`, `model`, `msdb` and `tempdb`. If I don't type in '.\SQLEXPRESS' there's only an option called "USER-PC" which is my PC-name. – Aske B. Sep 10 '12 at 07:38
  • Based on the folder paths the name of the instance you need to connect to may well be `MOBILIZEMESQL` – Martin Smith Sep 10 '12 at 07:41
  • 1
    @MartinSmith I didn't know what you meant by "instance" but I went to SQL Management Studio and saw that the server name I connect to is `USER-PC\MOBILIZEMESQL`. I tried entering that as the server name in VS and it worked, the `MobilizeMe`-database I was looking for showed up as a possible option in the "Connect to database"-box. Now it's connected so I'm guessing that is a proper step towards getting the SQL Server connected to Entity Framework. – Aske B. Sep 10 '12 at 08:02
  • Someapp has the file locked. Did you try to find out which one? Could be explorer.exe for all we know. Grab Process Explorer, do a file handle search for the database filename, and that will probably let you know your answer. If you cannot kill the other application, then you are going to have to connect to a copy of the file. Otherwise you'll have to share access to it. –  Sep 10 '12 at 12:36
  • 1
    @AskeB. I find [Unlocker](http://unlocker.soft32.com/free-download?gclid=COfsx_OKq7ICFcXe4Aod_xgAbg) is a good tool to find out what's locking a file on your machine – Rachel Sep 10 '12 at 13:10
  • Detach it from `SQL Server Object Explorer` was the solution for me. – Sebastian Xawery Wiśniowiecki Apr 18 '16 at 12:18
  • Yes you have to right click the file in server explorer and than use: modify connection... – Cerveser Mar 21 '17 at 14:44

0 Answers0