none
WPF/EF6 send updates back to the database from custom entities RRS feed

  • Question

  • Hi,
    i am filling my custom object entity in my viewmodel with a query like this :
    public DossierPatientClass patient { get; set; }
    .
    .
    .
           patient = (from n in ctx.PATIENTS
    
                                   .Where(a => a.ID_PATIENT == ID_PAT)
                                   select new DossierPatientClass()
                                   {
                                       ID_PATIENT = n.ID_PATIENT,
                                       PA_NOM = n.PA_NOM,
                                       PA_PRENOM = n.PA_PRENOM,
                                       PA_NOM_COMPLET = n.PA_NOM_COMPLET,
                                       PA_PHOTO = n.PA_PHOTO,
                                       PA_SEXE = (n.PA_SEXE).Value,
                                       PA_AGE = (n.PA_AGE).Value,
                                       PROBLEMS_LIST = new ObservableCollection<dynamic>(
                                           (from pb in n.PATIENTS_PROBLEMES
                                            select new
                                            {
    
                                                ID = pb.ID_PATIENT_PROBLEME,
                                                DESIGNATION = pb.PROBLEMES_TYPES.PRT_DESIGNATION,
                                                DEBUT = pb.PPB_DATE_DEBUT,
                                                DETAIL = pb.PPB_DETAILS,
                                                ANT = pb.PPB_ANTECEDANT
                                            }).ToList())
                                            ,
    
    
                                       TRAITEMENT_LIST = new ObservableCollection<dynamic>((from med in n.PATIENTS_MEDICAMENTS
                                                       select new
                                                       {
                                                           ID = med.ID_PAT_MED,
                                                           DESIGNATION= med.MEDICAMENT.MED_DESIGNATION
                                                       }).ToList()),
    
                                     CONSULTCNT = n.CONSULTATIONS.Count(),
                                     EXAMSCNT = n.CONSULTATIONS.SelectMany(t => t.EXAMENS).Count(),
                                     IMAGESCNT = n.IMAGES.Count()
    
                                 }).FirstOrDefault();

    wich give a read only and compact view of my object by returning only what i need from database as you see .and it works . also the query is in an async method so i have a responsive application ... etc .

    now is there a way to change the database if i change the custom object properties ?

    for example if i change the name property, or add some items to the ObservableCollection , how to make the updates in the database ?

    any idea is welcome, thanks .


    Wednesday, July 12, 2017 4:44 PM

Answers

  • now is there a way to change the database if i change the custom object properties ?

    You would need to get the old entity from the DB and map you properties from your custom object  properties to the like named properties in old object you just pulled from the DB. You change the state of the old object to 'modifed' and do the save.

     public void UpdateStudent(DTOStudent dto)
            {
                var student = new Student();
    
                using (var context = new CUDataEntities())
                {
                    student = (context.Students.Where(a => a.StudentID == dto.StudentID)).SingleOrDefault();
                }
    
                if (student != null)
                {
                    student.FirstName = dto.FirstName;
                    student.LastName = dto.LastName;
                    student.EnrollmentDate = dto.EnrollmentDate;
                } 
    
                using (var dbcontext = new CUDataEntities())
                {
                    if (student != null)
                    {
                        dbcontext.Entry(student).State = EntityState.Modified;
                        dbcontext.SaveChanges();
                    }
                }
            }

    http://www.entityframeworktutorial.net/EntityFramework4.3/update-entity-using-dbcontext.aspx

    • Marked as answer by issam1975 Wednesday, July 12, 2017 8:33 PM
    Wednesday, July 12, 2017 6:55 PM

All replies

  • now is there a way to change the database if i change the custom object properties ?

    You would need to get the old entity from the DB and map you properties from your custom object  properties to the like named properties in old object you just pulled from the DB. You change the state of the old object to 'modifed' and do the save.

     public void UpdateStudent(DTOStudent dto)
            {
                var student = new Student();
    
                using (var context = new CUDataEntities())
                {
                    student = (context.Students.Where(a => a.StudentID == dto.StudentID)).SingleOrDefault();
                }
    
                if (student != null)
                {
                    student.FirstName = dto.FirstName;
                    student.LastName = dto.LastName;
                    student.EnrollmentDate = dto.EnrollmentDate;
                } 
    
                using (var dbcontext = new CUDataEntities())
                {
                    if (student != null)
                    {
                        dbcontext.Entry(student).State = EntityState.Modified;
                        dbcontext.SaveChanges();
                    }
                }
            }

    http://www.entityframeworktutorial.net/EntityFramework4.3/update-entity-using-dbcontext.aspx

    • Marked as answer by issam1975 Wednesday, July 12, 2017 8:33 PM
    Wednesday, July 12, 2017 6:55 PM
  • Got the idea thanks :)

    but have another :p

    if i load the actual entity from database including all it's navigation properties (ObservableCollection)... etc

    using includes .

    do you think it will be problematic regarding performance ?

    i load just one entity at a time , in this case a patient with all it's examinations,medications, allergies... etc

    yes i am a little lazy sometimes ...

    Wednesday, July 12, 2017 8:40 PM
  • https://www.red-gate.com/simple-talk/dotnet/net-tools/entity-framework-performance-and-what-you-can-do-about-it/

    Wednesday, July 12, 2017 8:54 PM
  • nice reading for later ,

    last question  i promise :

    does EF core have a solution or better handling for this case ?

    Wednesday, July 12, 2017 8:59 PM
  • nice reading for later ,

    last question  i promise :

    does EF core have a solution or better handling for this case ?


    I don't know I have never been in the core! Just kidding if you know what the core means from military standpoint. I don't know what the .NET code does.
    Thursday, July 13, 2017 12:54 AM