Home > OS >  In Entity Framework, How to modify a list of objects stored in another class using System.Data.Entit
In Entity Framework, How to modify a list of objects stored in another class using System.Data.Entit

Time:11-11

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

  1. If I edit any field inside VitalID and write db.Entry(appt.VitalID).CurrentValues.SetValues(editedAppointment.VitalID); The changes are saved

  2. If 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;
  • Related