Map same Model class for multiple purposes in Entity FrameWork

asp.net asp.net-mvc c# entity-framework

Question

Two model classes are mine. one isApplicationUser the second is, too,Appointment . All people who use the application, in my case doctors and data entry workers, are considered application users. Each appointment will have a doctor assigned to it, and data entry workers will record this information in the database. Both of these users should be mapped to appointments. I've attempted something similar before.

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; }
}

But this generates a mistake.

Appointment_Doctor_Target_Appointment_Doctor_Source: : The types of all properties in the Dependent Role of a referential constraint must be the same as the corresponding property types in the Principal Role. The type of property 'DoctorID' on entity 'Appointment' does not match the type of property 'Id' on entity 'ApplicationUser' in the referential constraint 'Appointment_Doctor'.

Can somebody explain the cause of this mistake and the best course of action to take?

1
8
11/3/2016 2:11:12 PM

Accepted Answer

As with all entities in the asp.net identity entity architecture, IdentityUserstring as key. Your goal is to map to anint Use Guids as foreign keys in your Appointment entity instead, if possible.

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; }
}

or convert the Ids in identity classes to an int type. Help is available at here.

9
2/10/2015 8:13:17 AM

Popular Answer

ZZZ_tmp


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow