Adapt Idbset EF6 to EF Core

Asked

Viewed 103 times

0

I’m migrating from Entityframework 6x to Entityframework core, but I’m picking up a bit to implement an Idbset interface that doesn’t exist in EF Core. Instead of her, I need to use something like Mytext.set... but I don’t know how to do it properly. Someone knows how to help me?

inserir a descrição da imagem aqui

//My Repository Base

 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 Course Interface

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

//And finally, My Repository in which I need to use 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();
        }

  • In place of DBSet is dbContext.Curso.Include ....

  • He did not accept to use "dbContext". I declared a Property above the Constructor "protected readonly Sistemacomerciantext Context;" and Setei within the same " Context = dbContext;", but I don’t know if it would be correct...

  • Sistemacomercialcontext is the Entity Framework?:

  • Yes! It’s my Entityframework Core Context class.

  • 1

    It’s Context.Course ... Take the test

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.