Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC wants me to store my SQL Server database files in the App_Data folder -- should I?

When creating a new database with SQL Server Express 2005, the database files (.mdf and .ldf) get stored in C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data by default.

However, with the tutorials I've seen for ASP.NET MVC (e.g., Nerd Dinner), it seems to be common practice to keep the database files in the ASP.NET project's App_Data folder.

Questions

  1. Is there any significance to the App_Data folder, or is it just a convenient place to store database files if you happen to use Visual Studio's designer to create a new database?
  2. Will there be any negative repercussions if I don't use or even delete the App_Data folder?

Update

One thing I'm still not getting. If you have a production database on the server, why would you even want to replace this database with what is in App_Data. Wouldn't you normally just want to have update scripts that you run on the production database when you release a new version of the app? Even for initial deployment, I'd rather script database creation than physically copy over the files. Also, with SQL Server (Express) databases, copying is not enough. You have to detach the database to manipulate the files then reattach when you are done.

So, I have to say, the point of App_Data still escapes me. Can someone enlighten me?

like image 611
devuxer Avatar asked Jan 01 '10 22:01

devuxer


People also ask

What is the use of App_Data folder in MVC?

The App_Data folder of MVC application is used to contain the application related data files like . mdf files, LocalDB, and XML files, etc. The most important point that you need to remember is that IIS is never going to serve files from this App_Data folder.

What is App_Data folder in asp net?

App_Data contains application data files including . mdf database files, XML files, and other data store files. The App_Data folder is used by ASP.NET to store an application's local database, such as the database for maintaining membership and role information.

How can store data in database in ASP NET MVC?

Select “ Web application (Model-View-Controller)” template and press OK to create Asp.Net Core MVC project. Right-click on the solution and add a new folder named “Models”. Now, add a new class on the Models folder. Right-click on Models folder and select "Add a new class".


3 Answers

You can delete App_Data without any negative repercussions, but when it exists (by folder name) inside an ASP.NET website then it has the special website power of disallowing direct linking to download its contents - this is a security feature to protect your database from being downloaded directly over the web (e.g. by a web browser) even though it exists in the website. However your application can still access the files in the App_Data folder just as it accesses other website content.

Microsoft states it as:

Note: The content of application folders, except for the App_Themes folder, is not served in response to Web requests, but it can be accessed from application code.

Microsoft describes their special ASP.NET folder structures including App_Data here.

like image 92
John K Avatar answered Oct 09 '22 10:10

John K


There are a number of advantages of placing database files in the App_Data folder:

  1. As some have mentioned, that folder is secure from people browsing it directly on the web. This is also true of placing the database in folders outside of your web site, though.
  2. You can "xcopy deploy" your application by copying the entire folder from your local development machine to your hosting web site.
  3. Various components in Visual Studio can offer extra assistance in building your application by having your database files there. For example, you can double-click on a SQL Server Express MDF file and have it automatically open up in Server Explorer so that you can change the database's schema or view its data.
like image 44
Eilon Avatar answered Oct 09 '22 10:10

Eilon


There is absolutely no need to use the App_Data folder. It's just a convenient place to keep your database files together with your site. The decision to use it or not is more a matter of preference / policy than anything else.

like image 44
jeroenh Avatar answered Oct 09 '22 10:10

jeroenh