Doubt generating entity from the cs file code in edmx C #

2

In the ".Net Framework 4" version I have a project that when using .edmx made the automatic generation of EdmSchema where in my project I did so:

1 - ADO - (Class Library Project)

2 - .edmx file

3 - An Interface

using System;
using System.Linq;
using System.Linq.Expressions;

namespace LojaVirtual.DAO.Interfaces
{
    public interface IBaseCRUD<T>
    {

       void Adicionar(T pEntity);
       void Deletar(T pEntity);
       void Attach(T pEntity);
       void Detach(T pEntity);
       void Update(T pEntity);
       IQueryable<T> Selecionar(Expression<Func<T,bool>> where );
       IQueryable<T> SelecionarTodos();
       void Salvar();

    }
}

4 - An AbstratCrud:

using System;
using System.Linq;
using System.Linq.Expressions;
using LojaVirtual.DAO.Interfaces;

namespace LojaVirtual.DAO
{
    public  abstract class AbstractCRUD<T> :IBaseCRUD<T> where T:class 
    {
        LOJAEntities loja = new LOJAEntities();



        public void Adicionar(T pEntity)
        {
            loja.AddObject(pEntity.GetType().Name, pEntity);
        }

        public void Deletar(T pEntity)
        {
            loja.DeleteObject(pEntity);
        }

        public void Attach(T pEntity)
        {
           loja.AttachTo(pEntity.GetType().Name, pEntity);
        }

        public void Detach(T pEntity)
        {
            loja.Detach(pEntity);
        }

        public void Update(T pEntity)
        {
            loja.ApplyCurrentValues<T>(pEntity.GetType().Name, pEntity);
        }

        public IQueryable<T> Selecionar(Expression<Func<T, bool>> where)
        {
           return loja.CreateObjectSet<T>().Where(where);
        }

        public IQueryable<T> SelecionarTodos()
        {
            return loja.CreateObjectSet<T>();
        }

        public void Salvar()
        {
            loja.SaveChanges();
        }
    }
}

In the version of ".Net Framework 4.5" I have the same structure, but, my AbstractCRUD can not access the IBaseCRUD information

Error:

  

Error CS1061 'CRUD_MVCEntities' does not contain a definition for   'AddObject' and no extension method 'AddObject' accepting a first   argument of type 'CRUD_MVCEntities' could be found   using directive or an assembly   DAL C: \ Users \ name \ Downloads \ Project.FileInput \ DAL \ AbstractCRUD.cs 15 Active

Does anyone know why? Thank you

    
asked by anonymous 31.08.2018 / 04:09

1 answer

0

These methods, AddObject and DeleteObject , are used in older versions of EF and belong to ObjectContext , but in EF6, for example, we work with DbContext which has a different way of managing information.

You'll need to tailor your code to use DbContext :

public abstract class AbstractCRUD<C, T> : IBaseCRUD<T>
        where T : class
        where C : DbContext, new()
{
    private C _entities = new C();

    public C Context
    {
        get { return _entities; }
        set { _entities = value; }
    }

    public IEnumerable<T> ListarTodos()
    {
        IQueryable<T> query = Context.Set<T>();
        return query;
    }

    public IEnumerable<T> ProcurarPor(Expression<Func<T, bool>> predicate)
    {
        IQueryable<T> query = Context.Set<T>().Where(predicate);
        return query;
    }

    public void Adicionar(T entity)
    {
        Context.Set<T>().Add(entity);
    }

    public void Remover(T entity)
    {
        Context.Set<T>().Remove(entity);
    }

    public void Editar(T entity)
    {
        Context.Entry(entity).State = EntityState.Modified;
    }

    public void Gravar()
    {
        Context.SaveChanges();
    }
}

Now it's just a matter of making the adaptations you want, but in principle it will work without problems.

    
31.08.2018 / 10:46