Ejemplo n.º 1
0
 public void Eliminar(int id)
 {
     try
     {
         using (var ctx = new NetFramWorkContext())
         {
             ctx.Entry(new Alumno {
                 id = id
             }).State = EntityState.Deleted;
             ctx.SaveChanges();
         }
     }
     catch (Exception e)
     {
         throw new Exception(e.Message);
     }
 }
Ejemplo n.º 2
0
        public void Guardar()
        {
            try
            {
                using (var context = new NetFramWorkContext())
                {
                    if (this.id == 0)
                    {
                        context.Entry(this).State = EntityState.Added;
                    }
                    else
                    {
                        context.Database.ExecuteSqlCommand(
                            "DELETE FROM AlumnoCurso WHERE Alumno_id = @id",
                            new SqlParameter("id", this.id)
                            );

                        var cursoBK = this.Cursos;

                        this.Cursos = null;
                        context.Entry(this).State = EntityState.Modified;
                        this.Cursos = cursoBK;
                    }

                    foreach (var c in this.Cursos)
                    {
                        context.Entry(c).State = EntityState.Unchanged;
                    }

                    context.SaveChanges();
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }