繁体   English   中英

AspNetUsers链接到另一个表。 如何创建对象(1-1relationship)

[英]AspNetUsers link to another table. How to create object (1-1relationship)

我正在尝试创建一个包含其他用户信息的表,并将其与用户一对一关系链接。 我尝试了在stackOverflow上找到的一些东西,但仍然无法正确链接两个表(创建对象)

应用的想法是审查和评价美容治疗师。 用户可以选择是否想要一个扩展帐户作为治疗师(有额外的信息和可能被评级)。

我正在学习asp.net mvc,所以我愿意提供任何帮助或建议如何创建这个应用程序。

当用户登录并且我尝试创建Therapist对象时,我有一个错误(看起来我必须提供用户对象)。 不知道如何解决它,或者即使我的想法的解决方案是好的。

public class Therapist
{     
        public int IdTherapist { get; set; }

        [Key, ForeignKey("User")]
        [Required]
        public string UserId { get; set; }

        public virtual  ApplicationUser User { get; set; }

        [Required]
        [Display(Name = "Name")]
        public string TherapistName { get; set; }

        [Display(Name = "Surname")]
        public string TherapistSurname { get; set; }

        [Display(Name = "City")]
        public string City { get; set; } }

public class ApplicationUser : IdentityUser
{    

        public virtual Therapist Therapist { get; set; }
        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
        {

            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);

            return userIdentity;
        }
    }

//Therapist Controller

public ActionResult Create(Therapist therapist)      
   if (therapist.UserId == null)
               therapist.UserId = User.Identity.GetUserId();

                    _context.Therapists.Add(therapist);
                            context.SaveChanges();

                ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
            }

            return View(therapist);

运行此代码后,我有

“System.Data.Entity.Validation.DbEntityValidationException”,仔细看看:“ErrorMessage”用户字段是必需的。“
(不是User.Id)

尝试在User类中添加TherapistId属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM