public T GetById(int id)
 {
     using (var Db = new DavinciKlinicDBContext())
     {
         return(Db.Set <T>().FirstOrDefault(x => x.Id == id));
     }
 }
 public void Update(T entidad)
 {
     using (var db = new DavinciKlinicDBContext())
     {
         db.Entry(entidad).State = EntityState.Modified;
         db.SaveChanges();
     }
 }
 public void Insert(T entidad)
 {
     using (var Db = new DavinciKlinicDBContext())
     {
         Db.Entry(entidad).State = EntityState.Added;
         Db.SaveChanges();
     }
 }
 public void Delete(T Entidad)
 {
     using (var Db = new DavinciKlinicDBContext())
     {
         Db.Entry(Entidad).State = EntityState.Deleted;
         Db.SaveChanges();
     }
 }
 public IEnumerable <T> GetAll()
 {
     try
     {
         using (var context = new DavinciKlinicDBContext())
         {
             return(context.Set <T>().ToList());
         }
     }
     catch (Exception ex)
     {
         throw new Exception("No se pudieron recuperar los registros", ex);
     }
 }
Пример #6
0
        public void ClienteIntegrationTest()
        {
            //Crear DB
            var configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();

            var optionsBuilder = new DbContextOptionsBuilder <DavinciKlinicDBContext>();

            optionsBuilder.UseSqlServer(configuration["ConnectionStrings:DavinciKlinicConn"]);

            var context = new DavinciKlinicDBContext(optionsBuilder.Options);

            //Realizar Borrado de Todos Loc clientes Existentes en la BD
            //context.Clientes.RemoveRange(context.Clientes.ToArray());
            //context.SaveChanges();

            context.Database.EnsureDeleted();
            context.Database.EnsureCreated();

            //Crear Controllador
            ClientesController controller = new ClientesController(clientesOps);

            //Adicionar un Cliente
            controller.AddCliente(new Cliente()
            {
                CodigoCliente      = "09HZ",
                NombreCliente      = "Paten Huglin",
                Edad               = 20,
                DireccionCliente   = "48512 Ridgeway Point",
                TelefonoCliente    = "647 8987",
                DescripcionCliente = "Greece",
                FechaIngreso       = DateTime.Now,
                FacturaVenta       = null,
                Id            = 1,
                FechaRegistro = DateTime.Now,
                Eliminado     = false
            });

            //Chequear: que el Metodo GetAll Retorne el Cliente adicionado
            var result = (controller.GetCliente()).ToArray();

            Assert.Single(result);
            Assert.Equal("09HZ", result[0].CodigoCliente);
        }