12

I have made a project whit ASP.NET MVC using identity framework. It has created a new table AspNetUsers, but I want to add columns into that table like birth day, profile picture etc... How can I do that?

Can I run the query below in my SQL Server Management Studio?

ALTER TABLE AspNetUsers ADD ProfilePicture NVARCHAR(100);
ALTER TABLE AspNetUsers ADD BirthDay DATE;

Or is it more complex? I can not work with identity framework, so I must do it with ADO.NET and the .NET framework 4.

H. Pauwelyn
  • 13,575
  • 26
  • 81
  • 144
  • 3
    You will need to add the appropriate property to your `IdentityUser` class and then create an Entity Framework migration which will then run the appropriate SQL on your database when you execute the migration. http://blogs.msdn.com/b/webdev/archive/2013/10/16/customizing-profile-information-in-asp-net-identity-in-vs-2013-templates.aspx – James Nov 04 '15 at 10:11

2 Answers2

20

I have found it! @James suggested me an article that works.

Steps:

  1. Type in the console manager this code:

    Enable-Migrations
    

    Source: blogs.msdn.com

  2. In the class ApplicationUser add the property you want.

    public DateTime? Birthdate { get; set; }
    
  3. in the console manager type this code:

    Add-Migration "Birthdate"
    
  4. After it update the database whit this code:

    Update-Database
    

Result: a new column is added to the database whit name "Birthdate" and type datetime that can be null.

H. Pauwelyn
  • 13,575
  • 26
  • 81
  • 144
  • Thanks for the post.It helped me!In my case I had to add column of type int.I added the column as described on your post but, now I need to define the added int column as foreign key.Do you have any idea how can it be implemented? – Michael Feb 12 '17 at 11:42
  • @Michael take a look at this: https://stackoverflow.com/questions/5542864/how-should-i-declare-foreign-key-relationships-using-code-first-entity-framework – Chris Feb 25 '19 at 05:21
12

Suppose you want to add a new column named "FirstName":

Step 1: Models/IdentityModels.cs

Add the following code to the "ApplicationUser" class:

public string FirstName { get; set; }

Step 2: Models/AccountViewModels.cs

Add the following code to the "RegisterViewModel" class:

public string FirstName { get; set; }

Step 3: Views/Register.cshtml

Add FirstName input textbox to the view:

<div class="form-group">
        @Html.LabelFor(m => m.FirstName, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.TextBoxFor(m => m.FirstName, new { @class = "form-control" })
        </div>
</div>

Step 4 :

Go to Tools > NuGet Manager > Package Manager Console

Step A : Type “Enable-Migrations” and press enter
Step B : Type “ Add-Migration "FirstName" ” and press enter
Step C : Type “Update-Database” and press enter
i.e

PM> Enable-Migrations
PM> Add-Migration "FirstName"
PM> Update-Database

Step 5: Controllers/AccountController.cs

Go to Register Action and add "FirstName = model.FirstName" to the ApplicationUser i.e

var user = new ApplicationUser { UserName = model.Email, Email = model.Email, FirstName = model.FirstName}
Adrita Sharma
  • 21,581
  • 10
  • 69
  • 79