2

I'm currently using this connection string to attach to my database that I created in Visual Studio:

Data Source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|Database1.mdf;User Instance=true

I'm trying to host the site with IIS so I can mess around with response headers but I'm getting the problem described here: SQL Server Express connection string for Entity Framework Code First

I'm trying to find what database name to specify but not having any luck. I tried Initial Catalog=Database1 but that gave me this error:

Cannot create file 'D:\docs\Visual Studio 2010\Projects\QuickHomePage\QuickHomePage\App_Data\Database1.mdf' because it already exists. Change the file path or the file name, and retry the operation.
CREATE DATABASE failed. Some file names listed could not be created. Check related errors.

I'm just trying to attach to Database1.mdf. Why is it giving errors about trying to create it? One comment suggested attaching the .mdf file to another database instance to see what's inside it.

Would that require running SQL Server Management studio? Every time I try to connect to Server Type Database Engine and the local machine it gives a connection error.

1 Answer 1

3

The database name is the name you give your .MDF file as you attach it to the SQL Server (Express) server instance. There is no fixed database name "inside" the MDF that you need to discover - it's totally up to you what you call your database on the server.

So if you attach your Database1.mdf like this:

CREATE DATABASE CrazyDatabase ON
( FILENAME = N’C:\Data\Database1.mdf’ ),
( FILENAME = N’C:\Data\Database1_Log.ldf’ )
FOR ATTACH

then your database name is CrazyDatabase - but that has no connection whatsoever to the original MDF's file name or any contents inside it - you could call it anything else, too - whatever you choose.

In this case, your new connection string would be:

Server=.\SQLEXPRESS;Database=CrazyDatabase;Integrated Security=SSPI;
Sign up to request clarification or add additional context in comments.

3 Comments

So specifying both a .mdf file and a database is meaningless? In the process of attaching it to a "real" instance, I specify a database name? Does that mean I can't use an .mdf file with EF if I want to host through IIS?
@RandomEngy: yes - the "AttachDbFileName=" is intended for development purposes on your own dev machine only; once you go into production, you put the database onto a real SQL Server, attach it and from that point on, you only ever use that database name you've defined - the file name(s) becomes totally irrelevant. You should never define both in a connection string!
Thanks for clearing things up. I got things up and running nicely now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.