User456789
User456789

Reputation: 397

Expand the AspNetUsers by adding an extra model - code first

I want to update the AspNetUsers table and add custom properties. I made a new class called UserModel.

namespace theNotes.Models
{
 public class UserModel 
 {
    [Required]
    [Display(Name = "Education Level")]
    public string Grade { get; set; }

    public System.DateTime CreatedDate { get; set; }
    public bool IsActive { get; set; }
    public string IPAddress { get; set; }

    public Nullable<int> TotalFilesDownloaded { get; set; }
    public Nullable<int> FilesDownloadedToday { get; set; }
    public Nullable<decimal> Money { get; set; }
 }
}

When I run the command Add-Migration UserInfo in the PM a class is made with the methods up and down are empty. Why is it not generating the information of this class?

EDIT: I am using the asp.net mvc internet application, and what I want to do is add these columns to the AspNetUsers table that is automatically generated.

Upvotes: 0

Views: 501

Answers (3)

renakre
renakre

Reputation: 8291

You need to define your UserModel class like this:

[Table("AspNetUsers")]
public class UserModel : IdentityUser
{
   [Required]
   [Display(Name = "Education Level")]
   public string Grade { get; set; }

   public System.DateTime CreatedDate { get; set; }
   public bool IsActive { get; set; }
   public string IPAddress { get; set; }

   public Nullable<int> TotalFilesDownloaded { get; set; }
   public Nullable<int> FilesDownloadedToday { get; set; }
   public Nullable<decimal> Money { get; set; }
}

Upvotes: 0

IndieTech Solutions
IndieTech Solutions

Reputation: 2541

Few items are missing on your code:

  • You need to inherit fro the DBContext add this to your code where your UserModel class is:

    public class UserModelContext : DbContext { public DbSet<UserModel> UserModels{ get; set; } }

  • You need to run enable-migrations command for the new Context UserModelContext

    • Once you run the command you will get the error

EntityType 'UserModel' has no key defined. Define the key for this EntityType.

so you need to redesign your table and add a key.

  • You need to reference the foreign key to reference the User table

Upvotes: 0

Marcin Zablocki
Marcin Zablocki

Reputation: 10683

If you use Asp.net Identity, then you should add new properties in class ApplicationUser or similar one, which inherits from IdentityUser.

Upvotes: 1

Related Questions