如何在我的 Aspnetusers 表和自定义表之间建立关系



我已经扩展了我的Aspnetusers表,包括FirstNameMiddleNameLastNameStudentID

我想要一个简单的注册页面,在管理员中将创建学生帐户。然后学生帐户应该能够添加自己的个人联系方式。

现在,我创建了一个名为 "ContactDetails" 的自定义表,其中包含 ff:

public class Contact
{
    [Key]
    [Display(Name = "User Name")]
    public string UserName { get; set; }
    [Display(Name = "Contact Number")]
    public string StudentContactNumber { get; set; }
    [Display(Name = "Mother First Name")]
    public string MotherFirstName { get; set; }
    [Display(Name = "Mother Middle Name")]
    public string MotherMiddleName { get; set; }
    [Display(Name = "Mother Last Name")]
    public string MotherLastName { get; set; }
    [Display(Name = "Mother Contact Number")]
    public string MotherContactNumber { get; set; }
    [Display(Name = "Father First Name")]
    public string FatherFirstName { get; set; }
    [Display(Name = "Father Middle Name")]
    public string FatherMiddleName { get; set; }
    [Display(Name = "Father Last Name")]
    public string FatherLastName { get; set; }
    [Display(Name = "Father Contact Number")]
    public string FatherContactNumber { get; set; }
    [Display(Name = "Emergency First Name")]
    public string EmergencyFirstName { get; set; }
    [Display(Name = "Emergency Middle Name")]
    public string EmergencyMiddleName { get; set; }
    [Display(Name = "Emergency Last Name")]
    public string EmergencyLastName { get; set; }
    [Display(Name = "Emergency Contact Number")]
    public string EmergencyContactNumber { get; set; }

    public virtual ApplicationUser User { get; set; }
}

标识模型类:

public class ApplicationUser : IdentityUser
{
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }
    public string StudentID { get; set; }
    public string College { get; set; }
    public virtual System.Collections.Generic.ICollection<Contact> ContactInfo { get; set; }
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        return userIdentity;
    }

}

学生注册时,如何将我的用户(Aspnetusers.Id)放在联系人详细信息(User_Id)表中?我如何在他们之间建立关系?

我的管理员控制器:

 // GET: /Admin/Edit/Create 
    [Authorize(Roles = "Administrator")]
    #region public ActionResult Create()
    public ActionResult Create()
    {
        ExpandedUserDTO objExpandedUserDTO = new ExpandedUserDTO();
        ViewBag.Roles = GetAllRolesAsSelectList();
        return View(objExpandedUserDTO);
    }
    #endregion
    // PUT: /Admin/Create
    [Authorize(Roles = "Administrator")]
    [HttpPost]
    [ValidateAntiForgeryToken]
    #region 
    public ActionResult Create(ExpandedUserDTO paramExpandedUserDTO)
    {
        try
        {
            if (paramExpandedUserDTO == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            var Email = paramExpandedUserDTO.Email.Trim();
            var UserName = paramExpandedUserDTO.Email.Trim();
            var Password = paramExpandedUserDTO.Password.Trim();
            //
            var FirstName = paramExpandedUserDTO.FirstName.Trim();
            var MiddleName = paramExpandedUserDTO.MiddleName.Trim();
            var LastName = paramExpandedUserDTO.LastName.Trim();
            var StudentID = paramExpandedUserDTO.StudentID.Trim();
            var College = paramExpandedUserDTO.College.Trim();

            // UserName is LowerCase of the Email
            UserName = Email.ToLower();
            // Create user
            var objNewAdminUser = new ApplicationUser { UserName = UserName, Email = Email, FirstName = FirstName, MiddleName = MiddleName, LastName = LastName, StudentID = StudentID, College = College };             

            var AdminUserCreateResult = UserManager.Create(objNewAdminUser, Password);
            if (AdminUserCreateResult.Succeeded == true)
            {
                string strNewRole = Convert.ToString(Request.Form["Roles"]);
                if (strNewRole != "0")
                {
                    // Put user in role
                    UserManager.AddToRole(objNewAdminUser.Id, strNewRole);
                }
                return Redirect("~/Admin");
            }
            else
            {
                ViewBag.Roles = GetAllRolesAsSelectList();
                ModelState.AddModelError(string.Empty, "Error: User "+Email+ " already exists!");
                    //"Error: Failed to create the user. Check password requirements.");
                return View(paramExpandedUserDTO);
            }
        }
        catch (Exception ex)
        {
            ViewBag.Roles = GetAllRolesAsSelectList();
            //ModelState.AddModelError(string.Empty, "Error: " + ex);
            return View("Create");
        }
    }
    #endregion

不确定我该如何进行?提前谢谢。

Contact类中,你应该有这些:

public int UserId { get; set; }
public virtual ApplicationUser User { get; set; }

ApplicationUser课上,你应该有这些:

public int ContactId { get; set; }
public virtual Contact Contact { get; set; }

相关内容

  • 没有找到相关文章

最新更新