Adding Email, Details Field in UserProfile DB Table in MVC

In this article you will learn how to add Email and Details columns to an existing database. We get this database when running an application trying to create a new account. As you know, we don't see an email field in a user registration form as well as in the membership database. But we can enable this using some quick changes in the application. Read on.

 

In MVC we also have the opportunity to create a separate database table for users. In other words, we can add a new database table like "NewUsers" and we can hook this database table to membership from here (in the Filter > InitializeSimpleMembershipAttribute.cs file):


WebSecurity.InitializeDatabaseConnection("DefaultConnection""NewUsers""Id""Username", autoCreateTables: true);

 

Note: Remember to match "UserId" data in both tables "NewUsers" and "webpages_Membership", otherwise this will create a very big security hole in your application. Well that is not the subject of this article.
 

Now, let's go ahead and follow the steps to add Email and Details field to the database and update the Model, View and Controller to accept that data from the user.
 

 

Step 1: Update Database
 

Open the database and add the following fields.

 


Step 2: Update Data Model
 

Open the Data Model "AccountModels.cs", and add the following things in "RegisterModel".
 


Step 3: Update Controller
 

Open "AccountController.cs" and add the following code snippets:
 

 

Step 4: Update View
 

Now, open the "Register.cshtml" view and add the following code snippets:
 


Now, when you run the application you will see all in action.
 

What if one wants to let the user login using an email id?
 

Here it is, one quick change in the application will enable this feature.
 


I'm just instructing my application to use the "EmailId" field instead of the "UserName" field which is the default and I'm done.
 


Hope this helps.


Similar Articles