Advertisement
DimasGarcia

EstudianteDAL

May 22nd, 2021
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.85 KB | None | 0 0
  1. using ADSProjectLab.Models;
  2. using ADSProjectLab.Models.Context;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Data.Entity;
  6. using System.Linq;
  7. using System.Web;
  8.  
  9. namespace ADSProjectLab.DAL
  10. {
  11.     public class EstudianteDAL
  12.     {
  13.         private MyDbContext _context;
  14.  
  15.         public EstudianteDAL(MyDbContext context)
  16.         {
  17.             _context = context;
  18.         }
  19.  
  20.         // Se listan todos los Estudiantes
  21.         public List<Estudiante> ObtenerTodos()
  22.         {
  23.             try
  24.             {
  25.                 var listado = _context.Estudiante.ToList();
  26.                 return listado;
  27.             }
  28.             catch (Exception)
  29.             {
  30.                 throw;
  31.             }
  32.         }
  33.  
  34.         // Se listan todos los Estudiantes
  35.         public List<Estudiante> ObtenerTodos(string[] includes)
  36.         {
  37.             try
  38.             {
  39.                 var listado = _context.Estudiante.AsQueryable();
  40.                 foreach (var include in includes)
  41.                 {
  42.                     listado = listado.Include(include);
  43.                 }
  44.                 return listado.ToList();
  45.             }
  46.             catch (Exception)
  47.             {
  48.                 throw;
  49.             }
  50.         }
  51.  
  52.         // Se busca el Estudiante
  53.         public Estudiante ObtenerById(int Id)
  54.         {
  55.             try
  56.             {
  57.                 var item = _context.Estudiante.SingleOrDefault(x => x.Id == Id);
  58.                 return item;
  59.             }
  60.             catch (Exception ex)
  61.             {
  62.                 throw;
  63.             }
  64.         }
  65.  
  66.         // Se inserta el Estudiante
  67.         public int Insertar(Estudiante model)
  68.         {
  69.             try
  70.             {
  71.                 _context.Estudiante.Add(model);
  72.                 _context.SaveChanges();
  73.                 return model.Id;
  74.             }
  75.             catch (Exception ex)
  76.             {
  77.                 throw;
  78.             }
  79.         }
  80.  
  81.         // Se modifica el Estudiante
  82.         public int Modificar(int Id, Estudiante model)
  83.         {
  84.             try
  85.             {
  86.                 var currentItem = _context.Estudiante.SingleOrDefault(x => x.Id == Id);
  87.                 _context.Entry(currentItem).CurrentValues.SetValues(model);
  88.                 _context.SaveChanges();
  89.                 return model.Id;
  90.             }
  91.             catch (Exception ex)
  92.             {
  93.                 throw;
  94.             }
  95.         }
  96.  
  97.         // Se elimina el Estudiante
  98.         public bool Eliminar(int Id)
  99.         {
  100.             try
  101.             {
  102.                 var item = _context.Estudiante.SingleOrDefault(x => x.Id == Id);
  103.                 _context.Estudiante.Remove(item);
  104.                 _context.SaveChanges();
  105.                 return true;
  106.             }
  107.             catch (Exception ex)
  108.             {
  109.                 throw;
  110.             }
  111.         }
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement