Search code examples
c#visual-studio-2010asp.net-mvc-4

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.


Solution

  • 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.