示例#1
0
 public object Create(object p)
 {
     try
     {
         foreach (var item in (from t in context.Experimento where t.DataFim == null && t.DataInicio != ((Experimento)p).DataInicio select t).ToList <Experimento>())
         {
             item.DataFim = DateTime.Now;
             context.Entry(item).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
         }
         Experimento r = (context.Experimento.Add((Experimento)p)).Entity;
         context.SaveChanges();
         Program.experimentoAtivo = r.IdExperimento;
         return(r);
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
         return(null);
     }
 }
 public object Update(int idDadoColetado, object newObject)
 {
     try
     {
         DadoColetado r = (from p in context.DadoColetado where p.IdDadoColetado == idDadoColetado select p).FirstOrDefault <DadoColetado>();
         foreach (var att in ((DadoColetado)newObject).GetType().GetProperties())
         {
             if (!att.Name.Equals("idDadoColetado"))
             {
                 r.GetType().GetProperty(att.Name).SetValue(r, att.GetValue(newObject));
             }
         }
         context.Entry(r).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
         context.SaveChanges();
         return(r);
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
         return(null);
     }
 }
 public object Update(int IdGrandeza, object newObject)
 {
     try
     {
         Grandeza r = (from p in context.Grandeza where p.IdGrandeza == IdGrandeza select p).FirstOrDefault <Grandeza>();
         foreach (var att in ((Grandeza)newObject).GetType().GetProperties())
         {
             if (!att.Name.Equals("IdGrandeza"))
             {
                 r.GetType().GetProperty(att.Name).SetValue(r, att.GetValue(newObject));
             }
         }
         context.Entry(r).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
         context.SaveChanges();
         return(r);
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
         return(null);
     }
 }