Let ASP.NET 2.0 create "ASPNETDB.mdf" in my own database

2008-04-29


The ASP.NET version 2.0 membership feature provides secure credential storage for application users. It also provides a membership API that simplifies the task of validating user credentials when used with forms authentication.(MS)

when we create users and roles by selecting menu "ASP.NET Configration" wizard,  it will create a sql server express file named "aspnet.mdf", but most of time I have my own database for my projects, If I use these 2 seperat database, it will not convenient to deploy. so is it possible let asp.net 2 create those built-in tables, schemas, store procedure into my own database ?

I tried and I got successful. The following steps are what I did:

1: Create your own database in Visual Web Developer 2005 Express Edition using Database Explorer or Solution Explorer. i.e: its name is MyDB.mdf (this is a SQL Sqlsever instance database file);

2: Right click this database - MyDB.mdf, select "Modify Connection...";

3: You will see the Modify Connection window:

4: Change Data source, click the "Change..." button on the right side of Data Source textbox, select Microsoft SQL Server and click OK:

5: Then back to Mofify Connection Window, fill "[Your computer name]\SQLEXPRESS" into Server name textbox, if you have set authentication access to sql server express when you installed sQL Server Express, select "Use SQL Server Authentication", input Username and Password, Otherwise, just select "Use Windows Authentication";  and select "Attach a database file", click "Browse" butthon, locate your own database file that you created in Step 1 ("MyDB.mdf") and select it, click OK:

For my case, I just use Windows authentication.

6: Later , if you check your database by right click it, you will find "Data Source" has been changed to "Microsoft SQL Server Database File (SqlClient)" automatically, but I still have to do followed the above steps since I want to add my own database into [My machine name]\ExPRESS server;

7: Now, I can use "aspnet_regsql.exe" tool to create the built-in schema, tables and sprocs for the users and roles of ASP.NET 2.0 ;

locate the file "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regsql.exe" and double click it to run;

8: Next:

9: Change "Server" from the original text to "[Your Computer Name]\SQLEXPRESS" (My case is: 09B-036-D001\SQLEXPRESS)

10: Click Next until Finish:

11: Now I go back to my Visual Web Developer 2005 Express, I found my own database MyDB.mdf have been inserted some data tables, schema, store procedures... They just are what I mentioned in step 7.

12: Modify "connectionStrings" in web.config:

_ <connectionStrings> <!-- add name="LocalSqlServer" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/ --> <remove name="LocalSqlServer"/> <add name="LocalSqlServer" connectionString="Data Source=09B-036-D001\SQLEXPRESS;AttachDbFilename=C:\MyCodes\VisualStudio2005Express\WebSites\WebSite1\App_Data\mydb.mdf;Integrated Security=True" providerName="System.Data.SqlClient"/> </connectionStrings>_

Why remove LocalSqlServer just because this name has been added into machine.config in the folder "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG".

OK, now, I have finished my task : let asp.net 2.0 create the built-in data into my own SQL Server 2005 Express database. in fact, If I want to create those data into my own SQL Server 2005 database (not 2005 Express), it is easier than what I descripted above, because I dont need "record" my 2005 Express data file onto EXPRESS server, I can just start from step 7 (Or visit ScottGu's blg : http://weblogs.asp.net/scottgu/archive/2005/08/25/423703.aspx)