将 ASP.NET 核心标识用户属性保存在另一个表中

Geo*_*dal 7 sql-server asp.net-mvc asp.net-identity asp.net-core-mvc

我正在开发一个 ASP .NET 核心 Web 应用程序。我在应用程序中使用Identity UI 框架进行用户注册和授权。我将 Identity User 继承给我的ApplicationUser班级,如下所示。它创建了一个名为 as 的表ApplicationUser并成功保存了给定属性下的相关数据:(我正在使用Microsoft SQL数据库)

应用程序用户.cs

using Microsoft.AspNetCore.Identity;
using System;
using System.Collections.Generic;

#nullable disable

namespace WebApp.Models
{
        public partial class ApplicationUser : IdentityUser<int>
        {
            public ApplicationUser()
            {
            }

            public string FirstName { get; set; }
            public string MiddleName { get; set; }
            public string LastNam { get; set; }
            public DateTime? DateOfBirth { get; set; }
            public string Bio { get; set; }

            public virtual UserAddress UserAddress { get; set; }
        }
}
Run Code Online (Sandbox Code Playgroud)

然后我实现了UserAddress模型类如下。它在名为“UserAddress”的数据库中创建另一个表\

用户地址.cs

using System;
using System.Collections.Generic;

#nullable disable

namespace WebApp.Models
{
    public partial class UserAddress
    {
        public int UserId { get; set; }
        public string BuildingNo { get; set; }
        public string Street { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string ZipCode { get; set; }

        public virtual ApplicationUser User { get; set; }
    }
}
Run Code Online (Sandbox Code Playgroud)

接下来在Identity UI 框架中的Areas文件夹下,我按如下方式更改Index.cshtml文件。我插入新条目以输入building number用户,该条目应保存UserAddress在数据库的表中。

索引.cshtml

using Microsoft.AspNetCore.Identity;
using System;
using System.Collections.Generic;

#nullable disable

namespace WebApp.Models
{
        public partial class ApplicationUser : IdentityUser<int>
        {
            public ApplicationUser()
            {
            }

            public string FirstName { get; set; }
            public string MiddleName { get; set; }
            public string LastNam { get; set; }
            public DateTime? DateOfBirth { get; set; }
            public string Bio { get; set; }

            public virtual UserAddress UserAddress { get; set; }
        }
}
Run Code Online (Sandbox Code Playgroud)

Index.cshtml.cs文件中,我尝试将数据保存building numberUserAddress表中,如下所示,但未能将数据输入表中。

索引.cshtml.cs

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using WebApp.Models;

namespace WebApp.Areas.Identity.Pages.Account.Manage
{
    public partial class IndexModel : PageModel
    {
        private readonly UserManager<ApplicationUser> _userManager;
        private readonly SignInManager<ApplicationUser> _signInManager;

        public IndexModel(
            UserManager<ApplicationUser> userManager,
            SignInManager<ApplicationUser> signInManager)
        {
            _userManager = userManager;
            _signInManager = signInManager;
        }

        public string BuildingNo { get; set; }
        public string Username { get; set; }

        [TempData]
        public string StatusMessage { get; set; }

        [BindProperty]
        public InputModel Input { get; set; }

        public class InputModel
        {

            [DataType(DataType.Text)]
            [Display(Name = "User Name")]
            public string UserName { get; set; }

            [DataType(DataType.Text)]
            [Display(Name = "First name")]
            public string FirstName { get; set; }


            [DataType(DataType.Text)]
            [Display(Name = "Middle name")]
            public string MiddleName { get; set; }


            [DataType(DataType.Text)]
            [Display(Name = "Last name")]
            public string LastNam { get; set; }


            [Display(Name = "Date of Birth")]
            [DataType(DataType.Date)]
            public DateTime DateOfBirth { get; set; }

            [Phone]
            [Display(Name = "Phone number")]
            public string PhoneNumber { get; set; }

            [Display(Name = "Building No:")]
            [DataType(DataType.Text)]
            public string BuildingNo { get; set; }
        }

        private async Task LoadAsync(ApplicationUser user)
        {
            var userName = await _userManager.GetUserNameAsync(user);
            var phoneNumber = await _userManager.GetPhoneNumberAsync(user);
            Username = userName;
            Input = new InputModel
            {
                UserName = user.UserName,
                FirstName = user.FirstName,
                MiddleName = user.MiddleName,
                LastNam = user.LastNam,
                PhoneNumber = phoneNumber, 
            };
            
        }

        public async Task<IActionResult> OnGetAsync()
        {
            var user = await _userManager.GetUserAsync(User);
            if (user == null)
            {
                return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
            }

            await LoadAsync(user);
            return Page();
        }

        public async Task<IActionResult> OnPostAsync()
        {
            var user = await _userManager.GetUserAsync(User);
            if (user == null)
            {
                return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
            }

            if (!ModelState.IsValid)
            {
                await LoadAsync(user);
                return Page();
            }

            var phoneNumber = await _userManager.GetPhoneNumberAsync(user);
            if (Input.PhoneNumber != phoneNumber)
            {
                var setPhoneResult = await _userManager.SetPhoneNumberAsync(user, Input.PhoneNumber);
                if (!setPhoneResult.Succeeded)
                {
                    StatusMessage = "Unexpected error when trying to set phone number.";
                    return RedirectToPage();
                }
            }
            if(Input.FirstName != user.FirstName)
            {
                user.FirstName = Input.FirstName;
            }

            if (Input.MiddleName != user.MiddleName)
            {
                user.MiddleName = Input.MiddleName;
            }
            if (Input.LastNam != user.LastNam)
            {
                user.LastNam = Input.LastNam;
            }
            if (Input.DateOfBirth != user.DateOfBirth)
            {
                user.DateOfBirth = Input.DateOfBirth;
            }
            if (Input.UserName != user.UserName)
            {
                user.UserName = Input.UserName;
            }
            
            user.UserAddress.BuildingNo = Input.BuildingNo; // I tried to enter the building address to the UserAddress table by using this code

            await _userManager.UpdateAsync(user);

            await _signInManager.RefreshSignInAsync(user);
            StatusMessage = "Your profile has been updated";
            return RedirectToPage();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

请忽略Index.cshtml.cs我提供的上述冗长代码,我在我用来将输入插入building number到 UserAddress 表的唯一代码附近进行了评论 。即:
user.UserAddress.BuildingNo = Input.BuildingNo;

这是 条目视图的一部分:条目视图
这是我在运行上述代码时遇到 的错误错误消息

记录:ApplicationUser表中的所有字段都更新了。但是当我尝试将数据插入UserAddress表时会发生此错误。

我是漂亮的用户,对于非常了解 ASP.NET 核心身份用户的人来说,这是一个非常简单的问题。

我恳请是否有人可以帮助我将输入保存building number在另一个名为User Address?

提前致谢 !!!