Skip to content

Instantly share code, notes, and snippets.

@gzamudio
Last active December 18, 2018 18:00
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 gzamudio/d3e83df7c90fdfd9136d0451b62e2147 to your computer and use it in GitHub Desktop.
Save gzamudio/d3e83df7c90fdfd9136d0451b62e2147 to your computer and use it in GitHub Desktop.
EF Core
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Project.Repositories.Interfaces;
using Microsoft.EntityFrameworkCore;
namespace Project.Repositories
{
public class Repository<T> : IRepository<T> where T : class
{
private readonly DbContext _context;
public Repository(DbContext context) => _context = context;
public DbContext Context { get => _context; }
public void Add(T entity) => _context.Set<T>().Add(entity);
public void AddRange(IEnumerable<T> entities) => _context.Set<T>().AddRange(entities);
public T Get(int id) => _context.Set<T>().Find(id);
public IEnumerable<T> GetAll() => _context.Set<T>().ToList();
public void Remove(T entity) => _context.Set<T>().Remove(entity);
public void UpdateRange(IEnumerable<T> entities) => _context.Set<T>().UpdateRange(entities);
public bool Update(T entity)
{
var updateResult = _context.Set<T>().Update(entity);
return updateResult.State == EntityState.Modified;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment