Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using System.Linq;
- using Dapper;
- using Microsoft.EntityFrameworkCore;
- namespace MyProject
- {
- public class Repository<T> : IRepository<T> where T : class
- {
- private readonly DbContext _context;
- public Repository(DbContext context)
- {
- _context = context;
- }
- public IEnumerable<T> GetAll()
- {
- return _context.Set<T>().AsEnumerable();
- }
- public void Add(T entity)
- {
- _context.Set<T>().Add(entity);
- _context.SaveChanges();
- }
- public void Update(T entity)
- {
- _context.Set<T>().Update(entity);
- _context.SaveChanges();
- }
- public void Delete(T entity)
- {
- _context.Set<T>().Remove(entity);
- _context.SaveChanges();
- }
- public T GetById(int id)
- {
- return _context.Set<T>().Find(id);
- }
- public IEnumerable<T> GetByRawSql(string query, object parameters)
- {
- using (var connection = _context.Database.GetDbConnection())
- {
- return connection.Query<T>(query, parameters);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement