Skip to content

Instantly share code, notes, and snippets.

@icebeam7
Created May 11, 2018 12:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save icebeam7/e202f49e19dd7a16164f677dd3028571 to your computer and use it in GitHub Desktop.
Save icebeam7/e202f49e19dd7a16164f677dd3028571 to your computer and use it in GitHub Desktop.
SistemaEscolar: ServicioBaseDatos.cs
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using SistemaEscolar.Datos;
namespace SistemaEscolar.Servicios
{
public class ServicioBaseDatos<T> : IServicioBaseDatos<T> where T : class
{
BaseDatos bd;
public ServicioBaseDatos()
{
bd = App.BD;
}
public virtual async Task<List<T>> ObtenerTabla()
{
return await bd.Set<T>().ToListAsync();
}
public virtual async Task<T> BuscarPorId(int id)
{
return await bd.Set<T>().FindAsync(id);
}
public virtual async Task<T> Agregar(T dato)
{
await bd.Set<T>().AddAsync(dato);
await bd.SaveChangesAsync();
return dato;
}
public virtual async Task<T> Actualizar(T dato)
{
bd.Set<T>().Update(dato);
await bd.SaveChangesAsync();
return dato;
}
public virtual async Task<bool> Eliminar(int id)
{
var entity = await BuscarPorId(id);
bd.Set<T>().Remove(entity);
await bd.SaveChangesAsync();
return true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment