Last active
December 18, 2018 18:00
-
-
Save gzamudio/d3e83df7c90fdfd9136d0451b62e2147 to your computer and use it in GitHub Desktop.
EF Core
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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