在实体框架中为多个目的映射相同的Model类

Ath*_*hul 8 c# asp.net asp.net-mvc entity-framework

我有两个模型类ApplicationUser,第二个是Appointment.应用程序用户包括使用该应用程序的所有用户,在我的案例中,是医生和数据输入操作员.医生将被分配到每个约会,数据输入操作员将这个日志记录到DB.我想要预约这些用户.我尝试过这样的事情

public class Appointment
{
    public int AppointmentID { get; set; }
    public DateTime Date { get; set; }

    public int DoctorID { get; set; }
    [ForeignKey("DoctorID")]
    public virtual ApplicationUser Doctor { get; set; }

    public int SystemUserID { get; set; }
    public virtual ApplicationUser SystemUser { get; set; }
}

public class ApplicationUser : IdentityUser
{
    public string Email { get; set; }
    public string Mobile { get; set; }
    public string FirstNsme { get; set; }
    public string LastName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

但这会引发错误

Appointment_Doctor_Target_Appointment_Doctor_Source ::参照约束的从属角色中的所有属性的类型必须与主体角色中的相应属性类型相同.实体'Appointment'上的属性'DoctorID'的类型与参照约束'Appointment_Doctor'中实体'ApplicationUser'上的属性'Id'的类型不匹配.

任何人都可以指出为什么会出现此错误以及解决此问题的正确方法是什么?

tmg*_*tmg 9

IdentityUser作为asp.net身份实体框架中的所有实体都具有string关键.您正试图映射到int.因此,要么在指定实体中使用Guids作为外键

public class Appointment
{
    [Key]
    public int AppointmentID { get; set; }
    public DateTime Date { get; set; }

    public string DoctorID { get; set; }
    [ForeignKey("DoctorID")]
    public virtual ApplicationUser Doctor { get; set; }

    public string SystemUserID { get; set; }
    [ForeignKey("SystemUserID ")]
    public virtual ApplicationUser SystemUser { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

或者将标识类中的ID类型更改为int.你可以在这里找到帮助.