Adapt IDBSet EF6 to EF Core

0

I'm migrating from EntityFramework 6x to the core EntityFramework, but I'm catching up a bit to implement an IDBSet interface that does not exist in EF Core. In its place, I need to use something like MyContext.Set ... but I do not know how to do this correctly. Does anyone know how to help me?

//MyBaseRepository

 public class RepositoryBase<TEntity> : IRepositoryBase<TEntity> where TEntity : class
    {

        protected readonly SistemaComercialContext Context;

        public RepositoryBase(SistemaComercialContext dbContext)
        {
            Context = dbContext;
        }
        public void Add(TEntity obj)
        {
           Context.Set<TEntity>().Add(obj);
        }

        public TEntity GetById(Guid id)
        {
            return Context.Set<TEntity>().Find(id);
        }

        public virtual IEnumerable<TEntity> GetAll()
        {
            return Context.Set<TEntity>().ToList();
        }
        public virtual IEnumerable<TEntity> GetAllReadOnly()
        {
            return Context.Set<TEntity>().AsNoTracking();
        }
        public void Update(TEntity obj)
        {
            var entry = Context.Entry(obj);
            Context.Set<TEntity>().Attach(obj);
            entry.State = EntityState.Modified;
        }

        public void Remove(TEntity obj)
        {
            Context.Set<TEntity>().Remove(obj);
        }

        public IEnumerable<TEntity> Find(Expression<Func<TEntity, bool>> predicate)
        {
            return Context.Set<TEntity>().Where(predicate);
        }

        public void Dispose()
        {
            Context.Dispose();
            GC.SuppressFinalize(this);
        }
    }

// My Interface Course

public interface ICursoRepository : IRepositoryBase<Curso>
    {
        Curso ObterPorNome(string nome);
        Curso ObterPorIdComDependencias(Guid cursoId);
        IEnumerable<Curso> ObterGrid();
    }

// And finally, My Repository where I need to use the context

public class CursoRepository : RepositoryBase<Curso>, ICursoRepository
    {
        public CursoRepository(SistemaComercialContext dbContext)
            : base(dbContext)
        {

        }
        public IEnumerable<Curso> ObterGrid()
        {
                return DbSet
                    .Include("TipoCurso")
                    .AsNoTracking()
                    .ToList();
        }

        public Curso ObterPorIdComDependencias(Guid cursoId)
        {
            return DbSet
                 .Include("TipoCurso")
                 .FirstOrDefault(x => x.CursoId == cursoId);
        }

        public Curso ObterPorNome(string descricao)
        {
            return Find(x => x.Nome == descricao).FirstOrDefault();
        }
    
asked by anonymous 15.11.2017 / 20:09

0 answers