The property cannot be configured as a navigation property.

I am trying to implement a domain model as follows: My model

This is an mvc framework application. The model code is as follows:

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 training questions and, and no matter what I do, when I try to start 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<T> where T is a valid entity type 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<T> where T is a valid entity type 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<T> where T is a valid entity type . I don’t know what is wrong with TherapistId - maybe the whole idea of ​​the model is garbage?

+5
source share
1 answer

I think your specific mistake in this case is that you have

 [ForeignKey("TherapistId")] public int? TherapistId { get; set; } 

instead

 [ForeignKey("Therapist")] public int? TherapistId { get; set; } 

In any case, you seem to have a few problems, maybe something like this (I just saved the properties of the relations):

 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; } public virtual Login Login { get; set; } // was this supposed to be optional? [ForeignKey("Therapist")] public int? TherapistId{ get; set; } public virtual Therapist Therapist { get; set; } } public class Therapist { [Key] [ForeignKey("Login")] [Display(Name = "No.")] public int TherapistId { get; set; } public virtual Login Login { get; set; } public virtual ICollection<Patient> Patients { get; set; } } 

Typically, for one-to-one relationships in EF, both sides of the relationship must have the same primary key.


In fact, for what you are doing, perhaps you can try to use some kind of inheritance between Login , Patient and Therapist , because at the moment a Login can have both Patient and Therapist .

+13
source

All Articles