I am trying to implement a domain model like this:
This is an entity framework mvc application. The code for the model looks like this:
public class Login
{
[Key]
public int LoginID { get; set; }
public virtual Therapist Therapist { get; set; }
public virtual Patient Patient { get; set; }
}
public class Patient
{
[Key]
[ForeignKey("Login")]
[Display(Name = "No.")]
public int PatientId { get; set; }
[ForeignKey("Therapist")]
public int TherapistId { get; set; }
[ForeignKey("Therapist")]
public int TherapistId{ get; set; }
public virtual Therapist Therapist { get; set; }
public virtual Login Login { get; set; }
}
public class Therapist
{
[Key]
[ForeignKey("Login")]
[Display(Name = "No.")]
public int TherapistId { get; set; }
[ForeignKey("Login")]
public int LoginId { get; set; }
public virtual Login Login { get; set; }
public virtual ICollection<Patient> Patients { get; set; }
}
I follow exactly what is in the tutorials and stack overflow questions, and no matter what I do, when I try to run the controller, I get the same error:
Unable to retrieve metadata for 'Patient'. The property "TherapistId" cannot be configured as navigation property. The property must be a valid entity type and the property should have non-abstract getter and setter. For collection properties the type must implement ICollection where T is a valid entity type
I have no idea what is wrong with TherapistId
- maybe the whole idea of the model is rubbish?
Patient
class you are attaching theTherapist
ForeignKey toLoginId
. I think you want that onTherapistId
instead.[ForeignKey("Therapist")]