【问题标题】:How we add foreign key in asp.net identity MVC Project我们如何在 asp.net 身份 MVC 项目中添加外键
【发布时间】:2018-05-05 07:41:21
【问题描述】:

ApplicationUser 类继承 IdentityUser

public class ApplicationUser : IdentityUser
{

    public DateTime BirthDate { get; set; }
    public string User_type { get; set; }
    public DateTime CreatedOn { get; set; }
    public Enums.Sex Gender { get; set; }
    public bool IsActive { get; set; }
    public virtual Patient Patients { get; set; }
    public int PatientID { get; set; }

}

病人分类

public class Patient
{
    [Key]
    public int PatientID { get; set; }
    public string ProfilePicture { get; set; }
    public string FName { get; set; }
    public DateTime BirthDate { get; set;}
    public string City { get; set; }
    public string Country { get; set; }

}

控制器

 public async Task<ActionResult> RegisterPatient(RegisterViewModel model)
    {

        MYDb db = new MYDb();
        Patient pt = new Patient();
        pt.BirthDate = model.BirthDate;
        pt.City = model.City;
        pt.Country = model.Country;
        pt.ProfilePicture = model.ProfilePicture;
        pt.FName = model.FName;
        int Pat_id = model.PatientID;
        if (ModelState.IsValid)
        { 
            ApplicationUser myuser = new ApplicationUser();
            model.CreatedOn = DateTime.Now;
            model.IsActive = true;
            myuser.PatientID = Pat_id;
            if (ModelState.IsValid)
            {
                var user = model.GetUser();
                db.Patients.Add(pt);
                db.SaveChanges();
                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                { 
                    var idManager = new IdentityManager();
                    idManager.AddUserToRole(user.Id, model.User_type);
                    //await db.SaveChangesAsync();
                    await SignInAsync(user, isPersistent: false);
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    AddErrors(result);
                }
            }

            return RedirectToAction("Index", "Home");
        }

        // If we got this far, something failed, redisplay form
        return View();
    }

我想首先建立 b/w Patient 和 ApplicationUser 的关系 在患者中添加记录,然后将“Patient_ID”用作外键 ApplicationUser 表,但是当添加数据时我面临这种类型的错误

错误

注意:数据保存在 Patient 表中,但不保存在 ApplicationUser 表中,因为 ApplicationUser 没有得到“PatientID”,因此在数据库中发生了冲突。

我希望这很清楚并且不容易解决:(

【问题讨论】:

  • 该错误意味着您尝试将任何值作为 PatientID 插入到 ApplicationUser 中,该值不存在于患者表的 PatientID 列中。所以你需要调试你的代码并检查代码是否正确获取了 PatientID

标签: asp.net-mvc entity-framework-6 asp.net-identity asp.net-identity-2


【解决方案1】:

在应用程序用户模型中写这个:

public class ApplicationUser : IdentityUser
{
    public DateTime BirthDate { get; set; }
    public string User_type { get; set; }
    public DateTime CreatedOn { get; set; }
    public Enums.Sex Gender { get; set; }
    public bool IsActive { get; set; }
    [ForeignKey("PatientID")] 
    public virtual Patient Patients{ get; set; }
}

我认为它会起作用。

【讨论】:

  • 感谢回复,但有同样的问题记录插入患者表而不是 ApplicationUser :(
  • 您将数据插入到患者表中。并且您希望将 PatientID 作为外键放入 ApplicationUser 表中。我说的对吗?
  • 您会应用编辑后的代码吗?为什么在 Patient Class 中使用这条线? public ICollection&lt;ApplicationUser&gt; ApplicationUser { get; set; }
  • 好的,我删除了它,但面临同样的问题
  • 请大家帮帮我
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多