I want to update a List of Custom Objects stored in a class in entity Framework, But I'm running into this error: the entity type list`1 is not part of the model for the current context. I have observed what the problem is, but I don't have enough experience to solve this problem. Refer below code to get a better understanding of the issue at hand.
public class Appointment
{
public int AppointmentID { get; set; }
public int PetID { get; set; }
public int DoctorID { get; set; }
public DateTime AppointmentDate { get; set; }
public Status AppointmentStatus { get; set; }
public virtual List<ObservedPetIssue> ObservedPetIssueID { get; set; }
public string Reason { get; set; }
public virtual List<PrescribedMedicine> Prescription { get; set; }
public virtual List<DiagnosedSymptom> DiagnosedSymptomID { get; set; }
public virtual Vital VitalID { get; set; }
public virtual List<PrescribedTest> PrescribedTestID { get; set; }
public virtual List<Recommendation> RecommendationID { get; set; }
}
This is the class which has a list of other classes and one property which has a single class(Vital).
Here is the observation:
1)If I edit any field like PetID, DoctorID, or AppointmentStatus and write db.Entry(appt).CurrentValues.SetValues(editedAppointment);
The changes are saved
If I edit any field inside VitalID and write
db.Entry(appt.VitalID).CurrentValues.SetValues(editedAppointment.VitalID)
; The changes are savedIf I add any PrescribedMedicine to the Prescription list, or modify the existing PrescribedMedicine or not modify anything at all and write
db.Entry(appt.PrescribedTestID).CurrentValues.SetValues(editedAppointment.PrescribedTestID);
and error is throw stating: the entity type list`1 is not part of the model for the current context
I've Tried db.Entry(appt.Prescription).State = EntityState.Modified, still no success.
CodePudding user response:
That you do is similar that db.Entry(new List<PrescribedTest>())
. This fail because the db context don't know the entity type List<PrescribedTest>
.
This work with VitalID
because it's similar that db.Entry(new Vital())
and that success because the db context know the entity type Vital
.
If you want update a full entity collection, you can :
context.Entry(appointment).Collection(a => a.PrescribedTestID).CurrentValue = editedAppointment.PrescribedTestID;