Beispiel #1
0
        public void Edit(TEntity entity)
        {
            var entry = _context.Entry <TEntity>(entity);
            var pkey  = _dbSet.Create().GetType().GetProperty("id").GetValue(entity);

            if (entry.State == EntityState.Detached)
            {
                var     set            = _context.Set <TEntity>();
                TEntity attachedEntity = set.Find(pkey);  // access the key
                if (attachedEntity != null)
                {
                    var attachedEntry = _context.Entry(attachedEntity);
                    attachedEntry.CurrentValues.SetValues(entity);
                }
                else
                {
                    entry.State = EntityState.Modified; // attach the entity
                }
            }

            _context.SaveChanges();
        }
Beispiel #2
0
 public void ModifySubject(Subject modifiedSubject)
 {
     try
     {
         using (var context = new ContextDB())
         {
             context.Subjects.Attach(modifiedSubject);
             context.Entry(modifiedSubject).State = EntityState.Modified;
             context.SaveChanges();
         }
     }
     catch (Exception)
     {
         throw new SubjectPersistanceException("Se ha perdido la conexion con el servidor");
     }
 }
Beispiel #3
0
 private void ModifySchoolVan(SchoolVan modifiedSchoolVan)
 {
     try
     {
         using (var context = new ContextDB())
         {
             SchoolVan oldSchoolVan = Get(modifiedSchoolVan.Id);
             context.SchoolVans.Attach(oldSchoolVan);
             oldSchoolVan.Capacity             = modifiedSchoolVan.Capacity;
             context.Entry(oldSchoolVan).State = EntityState.Modified;
             context.SaveChanges();
         }
     }
     catch (Exception e)
     {
         throw new SchoolVanPersistanceException("Se ha perdido la conexion con el servidor");
     }
 }
Beispiel #4
0
 private void ModifyActivity(Activity modifiedActivity)
 {
     try
     {
         using (var context = new ContextDB())
         {
             Activity oldActivity = Get(modifiedActivity.Id);
             context.Activities.Attach(oldActivity);
             oldActivity.Name = modifiedActivity.Name;
             oldActivity.Cost = modifiedActivity.Cost;
             oldActivity.Date = modifiedActivity.Date;
             context.Entry(oldActivity).State = EntityState.Modified;
             context.SaveChanges();
         }
     }
     catch (Exception e)
     {
         throw new ActivityPersistanceException("Se ha perdido la conexion con el servidor");
     }
 }
Beispiel #5
0
 public void ModifyTeacher(Teacher modifiedTeacher)
 {
     try
     {
         using (var context = new ContextDB())
         {
             Teacher oldTeacher = Get(modifiedTeacher.Id);
             context.Teachers.Attach(oldTeacher);
             oldTeacher.Id                   = modifiedTeacher.Id;
             oldTeacher.Name                 = modifiedTeacher.Name;
             oldTeacher.LastName             = modifiedTeacher.LastName;
             oldTeacher.Subjects             = modifiedTeacher.Subjects;
             oldTeacher.Subjects             = GetSubjectListAttached(context, modifiedTeacher);
             context.Entry(oldTeacher).State = EntityState.Modified;
             context.SaveChanges();
         }
     }
     catch (Exception e)
     {
         throw new StudentPersistanceException("Se ha perdido la conexion con el servidor");
     }
 }
Beispiel #6
0
 private void ModifyStudent(Student modifiedStudent)
 {
     try
     {
         using (var context = new ContextDB())
         {
             Student oldStudent = Get(modifiedStudent.Id);
             context.Students.Attach(oldStudent);
             oldStudent.Name                 = modifiedStudent.Name;
             oldStudent.LastName             = modifiedStudent.LastName;
             oldStudent.StudentNumber        = modifiedStudent.StudentNumber;
             oldStudent.Coordinates          = modifiedStudent.Coordinates;
             oldStudent.Ci                   = modifiedStudent.Ci;
             oldStudent.Subjects             = modifiedStudent.Subjects;
             oldStudent.Subjects             = GetSubjectListAttached(context, modifiedStudent);
             context.Entry(oldStudent).State = EntityState.Modified;
             context.SaveChanges();
         }
     }
     catch (Exception e)
     {
         throw new StudentPersistanceException("Se ha perdido la conexion con el servidor");
     }
 }