How to add custom table in ASP.NET IDENTITY?

2019-02-16 12:48发布

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.

2条回答
小情绪 Triste *
2楼-- · 2019-02-16 13:10

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!

查看更多
ゆ 、 Hurt°
3楼-- · 2019-02-16 13:14
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; }
查看更多
登录 后发表回答