Creating ASP.NET WebApplication/ASP.NET MVC Application Membership Database on SQL Server rather than LocalDb

I was asked this question from one of my blog reader: How can a DB be created in SQL Server rather than locally (LocalDb) which is default?

When you create new ASP.NET MVC Application (using regular template ‘Internet’) or ASP.NET WebApplication, you will notice the following connectionString in Web.config file:

<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-MvcApplication1-20121005163323;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-MvcApplication1-20121005163323.mdf" providerName="System.Data.SqlClient" />

Actually this helps MVC Application or ASP.NET WebApplication to generate database locally (within project in App_Data folder) at run-time for accounts/login management (membership). If however you want to generate this database in SQL Server then you can make some quick changes in the above Web.config file to do that, so here is what you need to replace in the above db mapping.

<add name="DefaultConnection" connectionString="Data Source=ITORIAN-PC;Initial Catalog=ASPNETMembership;Integrated Security=True;MultipleActiveResultSets=True;" providerName="System.Data.SqlClient" />

You probably have a different data sources so change it before running. Now, run the application and click on ‘Register’ link to create an account and then you are all done.


Open the SQL Server Management Studio and look at the generated DB for this application.

At this stage if you run your application you will get following error:

Login failed for user IIS APPPOOL\*

To fix this you need to update IIS Process Model Identity in Application Application Pool.


Hope this helps. Thanks.

Comments

  1. I was struggling to create the users in sqlexpress but it was being created in localDB by default. Your post helped me create the users profile and other membership tables in sql express.
    Many many thanks for your advice.

    ReplyDelete
  2. In this application I registered and in the data base i added new table with the fields that contains some and we have created views like list,create,edit etc., if log in into the application then list view should display only my record not others and should we implement this

    ReplyDelete

Post a Comment

Popular posts from this blog

Migrating database from ASP.NET Identity to ASP.NET Core Identity

Customize User's Profile in ASP.NET Identity System