public List <Cursos> ListarCursos() { var cursos = new List <Cursos>(); try { using (var context = new BaseDeDatosContext { }) { cursos = context.Cursos.ToList(); } } catch (Exception e) { throw new Exception(e.Message); } return(cursos); }
public List <Alumnos> Listar() { var alumnos = new List <Alumnos>(); try { using (var context = new BaseDeDatosContext { }) { alumnos = context.Alumnos.ToList(); } } catch (Exception e) { throw new Exception(e.Message); } return(alumnos); }
//public Alumnos Obtener(int id) //{ // var alumno = new Alumnos(); // try // { // using (var context = new BaseDeDatosContext()) // { // alumno = context.Alumnos // .Include("Ciudades") // .Where(x => x.Alumno_Id == id) // .Single(); // } // } // catch (Exception e) // { // throw new Exception(e.Message); // } // return alumno; //} public Alumnos Obtener(int id) { var alumno = new Alumnos(); try { using (var context = new BaseDeDatosContext()) { alumno = context.Alumnos .Include("Ciudades").Include("Cursos") .Where(x => x.Alumno_Id == id) .Single(); } } catch (Exception e) { throw new Exception(e.Message); } return(alumno); }
//public void Guardar() //{ // try // { // using (var context = new BaseDeDatosContext()) // { // if (this.Alumno_Id == 0) // { // context.Entry(this).State = EntityState.Added; // foreach (var c in this.Cursos) // { // context.Entry(c).State = EntityState.Unchanged; // } // } // } // } // catch (Exception e) // { // throw new Exception(e.Message); // } //} public void Guardar() { try { using (var context = new BaseDeDatosContext()) { if (this.Alumno_Id == 0) { context.Entry(this).State = EntityState.Added; } else { context.Database.ExecuteSqlCommand( "DELETE FROM AlumnosCursos WHERE Alumno_id = @Alumno_id", new SqlParameter("Alumno_Id", this.Alumno_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); } }