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); } }
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); } }