I’ve always wondered why the ASP.NET folks have always had (or at least since VS2005) the ability to add a detached SQL Express file, or commonly referred to as an MDF file, whereas, project types like console application and windows forms do not.
When you add a new item to an ASP.NET application you will see the following dialog.
Notice the SQL Server Database option. Selecting this will cause an MDF file to be added to the ASP.NET application. Very convenient. However, no such option exists for either the console or windows forms project types.
I have seen developers add MDF files by starting SQL Server Management Studio, creating a database, detaching it and then coping the file to the project. I have also seen developers create an ASP.NET project, add a MDF file and then copy the MDF to the desired project. Neither are optimal.
One great way to automate this, is to create an Item Template. To do this create an ASP.NET project and then add an MDF file by selecting Add New Item menu option and selecting SQL Server Database. This will add a detached SQLExpress database file to the project. The next step is to delete the LDF (log) file from the project.
Now we are ready to create the Item Template. Select the File->Export Template menu. This causes the Export Template Wizard to appear,a s shown below.
Select the Item template radio button and click the Next > button. The Select Item To Export dialog appears from which you select the MDF file.
The next dialog to appear after selecting the Next > button is the Select Item References dialog. In this situation there is no need to include any references so just click the Next > button. The Select Template Options dialog appears from which you specific the name of the template. When done select Finish and that’s it you have created a item template that can be used just about everywhere.
Try it out, create a console application and then select the Add->New Item menu item. If all goes well you should see the Add New Item dialog and under the My Templates you should see the new Item Template. Selecting this Item Template will add a SQLExpress MDF file to the project.
