12

I'm using ASP.NET Identity on my web form application. Below is my current identity tables:

Current Identity tables

 - Role
 - User
 - UserClaim
 - UserLogin
 - UserRole

I need to add a new table to store additional information.

New Table: UserLogs

Fields inside the new table:

  • UserLogID (PK)
  • UserID (FK)
  • IPAddress
  • LoginDate

How can I add this custom table? I know how to add custom fields inside existing table but I don't know how to achieve this.

I appreciate your efforts in reaching a solution for my problem.

3
  • possible duplicate of Extending asp.net mvc 5 identity with custom tables Check the answer I think it will be enough. Commented Aug 11, 2015 at 12:27
  • Let me check then I will let you know. Thanks Commented Aug 11, 2015 at 13:00
  • @mybirthname I have a question on this line. public virtual ApplicationUser User { get; set; } , Here I should add my column which it exists in my Log Table correct? Commented Aug 11, 2015 at 13:14

2 Answers 2

11
public class ApplicationUser : IdentityUser
{

    public virtual ICollection<UserLog> UserLogs { get; set; }

}

public class UserLog
{
    [Key]
    public Guid UserLogID { get; set; }

    public string IPAD { get; set; }
    public DateTime LoginDate { get; set; }
    public string UserId { get; set; }

    [ForeignKey("UserId")]
    public virtual ApplicationUser User { get; set; }
}

public System.Data.Entity.DbSet<UserLog> UserLog { get; set; }
Sign up to request clarification or add additional context in comments.

1 Comment

i have executed the Identity 2.0 script to add the tables to my custom DB. Then, i added the model using DB first approach. Now, i am trying to add columns as you already did. I added the column first to ASPNetUser table, then added the property to Application User, then update the model. However, it generates an error. Can you please let me know about the steps and sequence?
2

first of all I'd suggest you to understand a bit about Code-First Entity Framework, because that's how those tables about users is created when you first create New MVC5 Applictation.

Here you can find how it is implemented if you want to use code-first database creating through the EntityFramework.

After you understand some core stuff about it, you can go to your project on Models folder, straight to> AccountModel.cs

There you have a Class which extends DbContext, which inside contains the constructor which gives access to the connectionstring on which database is about to be used.

Short important stuff if u are lazy on reading code first too much:

DbSet means that the model inside dbset, is about to be created into a table. Variables inside class, are columns that are about to be created. But if you want foreign keys and stuff, you definitely have to read code-first kind of.

Good Luck!

5 Comments

Thanks for this useful article but I'm not using MVC, it's (Web Forms).
Sir, as far as I know and seen, the same structure with EntityFramework is also used in Webforms. I'd suggest you confirm it goes almost the same way. If not, then I'm sorry, and i hope you find soon what you're looking for.
I agree with you but there is no AccountModel.cs in my project. Under Models I have IdentityModels.cs.
That is because you are using Identity 2.0 @KevinMaxwell.
@scheien I assume Identity 2.0 is the latest? correct?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.