Link ASP.NET Identity users to user detail table

后端 未结 5 2086
旧巷少年郎
旧巷少年郎 2020-12-29 16:46

I\'ve used default MVC template with individual authorization. After running the application it automatically creates the required Identity tables. I\'ve successfully regist

5条回答
  •  梦毁少年i
    2020-12-29 17:08

    Yes, you should add a foreign key to your table. How to do so depends on how you set up your Entity Framework (EDMX / code first / reverse engineered from database), but this proceduree is explained in plenty other questions. That is a separate question and that has been answered before, try searching.

    Now to store the details for a user, you can either create a record in your PersonalInformation table upon registering this user, or checking if it exists when editing a user:

    var currentUserId = User.Identity.GetUserId();
    
    // Look up existing record
    var personalInformation = _dbContext.PersonalInformation
                                        .FirstOrDefault(p => p.UserId == currentUserId);
    
    if (personalInformation == null)
    {
        // If not exists, create and attach new record
        personalInformation = _dbContext.PersonalInformation.Create();  
    }
    
    // Map incoming model properties to entity
    
    personalInformation.FirstName = model.FirstName;
    personalInformation.Address = model.Address;
    
    // ...
    
    // Add or update the entity in the database
    _dbContext.SaveChanges();
    

提交回复
热议问题