Doubt generating entity from Cs file code in edmx C#

Asked

Viewed 40 times

2

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

1 - ADO - (Project Class Library)

2 - Archive . edmx

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 Abstract:

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, more , my Abstractcrud can not access the information of Ibasecrud

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 (are you Missing a using Directive or an Assembly Reference? ) DAL C: Users name Downloads Project.Fileinput DAL Abstractcrud.Cs 15 Active

Does anyone know why? Thanks

1 answer

0

That method, AddObject and DeleteObject, are used in older versions of EPH and belong to the ObjectContext, but at EF6, for example, we work with a DbContext, which has a different way of managing information.

You will need to adapt your code to use the 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 whatever adjustments you want, but in principle it will work smoothly.

Browser other questions tagged

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