Add foreign key to AspNetUser table

前端 未结 1 1176
眼角桃花
眼角桃花 2021-01-29 12:09

I\'ve created an ASP.NET Core Razor Pages application with user identities.

The created project includes a migration that adds all the user tables, such as AspNetU

1条回答
  •  北恋
    北恋 (楼主)
    2021-01-29 12:20

    The default identity uses IdentityUser, it is not exposed to the project code but you can create a new user model (e.g. AppUser) that is derived from IdentityUser, then add the foreign keys to the new user model:

    public class AppUser : IdentityUser
    {
        public int AddressId { get; set; }
        public UserAddress Address { get; set; }
    }
    
    public class UserAddress
    {
        public int Id { get; set; }
    
        // ...
    
        public string UserId { get; set; }
        public AppUser User { get; set; }
    }
    

    Then replace IdentityUser in startup with AppUser :

    services.AddDefaultIdentity();
    

    Then create new migrations to create the new user table.

    0 讨论(0)
提交回复
热议问题