mkell
mkell

Reputation: 731

Why is mdf file not appearing in the App_Data folder?

I've being trying to get started with Beginning ASP.NET MVC 4.

And hit a problem straight away, according to the e-book I should be able to start a new mvc 4 internet application, debug and select log-in form the UI tempate, then stop. This should create the mdf file in the App_Data folder. Which I should be able to click on and open in Server Explorer.

What actually happens - Database get created in my SQL Express.

Getting this step correct is crucial to following the rest of the tutorial.

I am familiar with MVC , but mainly the front end stuff, so I'm trying to increase my understanding of the database/models side of MVC.

This is a completely new project, no changes were made, just out of the box code. On a windows XP machine, using Visual Studio 2010.

Connectionstring -

  <add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=aspnet-HaveYouSeenMe-20131125091100;Integrated Security=SSPI" providerName="System.Data.SqlClient" />

Can anyone please explain - why my out of the box connectionstring is creating the Db in SQL Express and how to create the mdf in the App_Data folder as book suggests. cheers.

Upvotes: 9

Views: 25570

Answers (8)

Oleksiy
Oleksiy

Reputation: 39790

Do you have an App_Data folder? In my case, it was showing up in Visual Studio but it did not actually exist. This was because git does not push empty directories, so cloning the repo into another location would result in no App_Data folder. Once I created it manually, the issue was resolved.

Upvotes: 0

Mark
Mark

Reputation: 93

The database most likely has been created in your Documents folder. In the Server Explorer right click on the Database (in Data Connections) and click Browse In SQL Server Object Explorer. Then in SQL Server Object Explorer right click on your database and select Properties. In the Properties window look up Current Connection Parameters and see the value for Data File.

To ensure the database is created in your App_Data folder insert AttachDbFilename=|DataDirectory|\database_name.mdf in your connection string.

Upvotes: 1

Willy David Jr
Willy David Jr

Reputation: 9131

It took me sometime as well to figure this out since I want to attach the database under App_Data and nothing is happening. No files were added. I just figure it out that there are two options to click the Show All Files.

On this picture, I was clicking the Red arrow which says Show All Files. After sometime, I tried to click the other Show All Files on the Blue arrow and it now works. enter image description here

I hope that might help if you encounter the same problem.

Upvotes: 5

Suleman Ahmed
Suleman Ahmed

Reputation: 1

You can also go into the server explorer and right click on the data connections. Then press "add connections" and change the data source to be "server database file" and under database file name(new or existing) you go in the app_data folder, if you cant find the .mdf file, then browse to that folder and add the name you want to give your file.

`SulemansStore\SulemansStore\App_Data\SulemansDatabase.mdf`

I browse to App_Data and add my databasefile name Sulemansdatabase.mdf (this part I added if I am creating a new database file).

Upvotes: 0

Devy
Devy

Reputation: 1

In my case, it was because the user account that I use to login to my PC is Microsoft account (Outlook email address), while Visual Studio read my local user account. The local user account is the "userName" in C:\Users\userName\Documents\Visual Studio 2015\Projects\ProjectFolder\App_Data.

I'm using Visual Studio 2015 in Windows 10.

The solution turns out to be simple. I just need to go to my PC Settings, select Accounts and choose to sign in with local account instead of Microsoft account.

Upvotes: 0

Michael Elliott
Michael Elliott

Reputation: 241

I know this says answered, but it may be as simple as clicking the "show all files" icon in the Solution Explorer. By default the .mdf file will not show up, so ensure the "show all files" is selected :)

Upvotes: 24

Nagaraj S
Nagaraj S

Reputation: 13474

You are missing AttachDbFilename

 <connectionstrings>
            <add connectionstring="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\aspnet-HaveYouSeenMe-20131125091100.mdf;Integrated Security=True;User Instance=True  " 
name="ConnectionASPX" providername="System.Data.SqlClient" />
    </connectionstrings>

In place of the physical path to a database file that is stored in the App_Data folder, you can specify the |DataDirectory| variable in the AttachDbFileName setting.

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

Just change your connection string:

<add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\aspnet-HaveYouSeenMe-20131125091100.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>

The |DataDirectory| is a special token which is pointing to the ~/App_Data folder of your application.

Checkout the following article on MSDN which provides more details about connection strings in SQLExpress.

Upvotes: 4

Related Questions