Márcio Eric
Márcio Eric

Reputation: 205

ObjectDisposedException EF Error

I´m having a problem because always I press the Action Button I got the this Exception.

This error

This part is giving me a error

public void InserirComCopia(TabelaPreco tabPrec, string copiarDe)
    {
        var lista = new List<PrecoProduto>();
        var tb = InserirTabelaVazia(tabPrec);
        var rep = new PrecoRepositorio();
        lista = rep.ObterPrecoPorTabela(Int32.Parse(copiarDe));
        var ls = new List<PrecoProduto>();

        using (var context = new indigo.DataModel.IndigoContext())
        {
            foreach (var item in lista)
            {
                var p = new PrecoProduto()
                {
                    preco = item.preco,
                    TabPreco = tb,
                    TabPrecoId = tb.Id,
                    Produto = item.Produto,
                    ProdutoId = item.ProdutoId
                };
                ls.Add(p);
            }
            context.PrecoProduto.AddRange(ls);
            context.SaveChanges();
        }
    }

And this is all my controller:

public TabelaPreco ObterTablePrecoPorID(int Id, List<TabelaPreco> tabelaPreco)
    {
        return tabelaPreco.Where(t => t.Id == Id)
                          .FirstOrDefault();
    }
    public List<TabelaPreco> ObterTodasAsTabelas()
    {
        List<TabelaPreco> model = new List<TabelaPreco>();
        using (var context = new indigo.DataModel.IndigoContext())
        {
            model = context.TabelaPreco.ToList();
        }
        return model;
    }
    public List<TabelaPreco> Buscar(string busca)
    {
        List<TabelaPreco> model = new List<TabelaPreco>();
        using (var context = new indigo.DataModel.IndigoContext())
        {
            model = context.TabelaPreco.Where(tb => tb.Desc.Contains(busca)).ToList();
        }
        return model;
    }
    public TabelaPreco InserirTabelaVazia(TabelaPreco tab)
    {
        using (var context = new indigo.DataModel.IndigoContext())
        {
            context.TabelaPreco.Add(tab);
            context.SaveChanges();
            return tab;
        }
    }
    public void Deletar(int id)
    {
        var tabela = new TabelaPreco();
        using (var context = new indigo.DataModel.IndigoContext())
        {
            tabela = context.TabelaPreco.Where(tb => tb.Id == id)
                                        .FirstOrDefault();
            context.TabelaPreco.Remove(tabela);
            context.SaveChanges();
        }
    }
    public void InserirComCopia(TabelaPreco tabPrec, string copiarDe)
    {
        var lista = new List<PrecoProduto>();
        var tb = InserirTabelaVazia(tabPrec);
        var rep = new PrecoRepositorio();
        lista = rep.ObterPrecoPorTabela(Int32.Parse(copiarDe));
        var ls = new List<PrecoProduto>();

        using (var context = new indigo.DataModel.IndigoContext())
        {
            foreach (var item in lista)
            {
                var p = new PrecoProduto()
                {
                    preco = item.preco,
                    TabPreco = tb,
                    TabPrecoId = tb.Id,
                    Produto = item.Produto,
                    ProdutoId = item.ProdutoId
                };
                ls.Add(p);
            }
            context.PrecoProduto.AddRange(ls);
            context.SaveChanges();
        }
    }
    public TabProdListModel PegarProdutosDaTabela(int id)
    {
        using (var context = new indigo.DataModel.IndigoContext())
        {
            var modelTab = context.TabelaPreco.Where(tb => tb.Id == id).First();
            var modelProd = context.Produto.ToList();
            var model = context.TabelaPreco
                           .Where(t => t.Id == id)
                           .Join(
                               context.PrecoProduto,
                               t => t.Id,
                               x => x.TabPrecoId,
                               (t, x) => new { t, x }
                           )
                           .Join(
                               context.Produto,
                               p => p.x.ProdutoId,
                               y => y.Id,
                               (p, y) => new ListProduto
                               {
                                   produtoId = y.Id,
                                   produto = y.Nome,
                                   precoProduto = p.x.preco,
                                   Cor = y.Cor,
                                   Tamanho = y.Tamanho
                               }
                           )
                           .ToList();

            var ls = new TabProdListModel()
            {
                tabela = modelTab,
                Produtos = modelProd,
                TdProdutos = model
            };

            var prod = ls.Produtos.ToList();
            if (modelProd.Count() != 0)
                foreach (var item in ls.Produtos)
                {
                    foreach (var td in ls.TdProdutos)
                    {
                        if (item.Id == td.produtoId)
                        {
                            prod.Remove(item);
                            break;
                        }

                    }
                }

            ls.Produtos = prod;
            return ls;
        }
    }
    public void AdicionarProdTab(int Produto, double Valor, int Tabela)
    {
        using(var context = new indigo.DataModel.IndigoContext())
        {
            var produto = context.Produto.Where(p => p.Id == Produto).FirstOrDefault();
            var tabela = context.TabelaPreco.Where(tp => tp.Id == Tabela).FirstOrDefault();
            var precoProduto = new PrecoProduto()
            {
                preco = Valor,
                Produto = produto,
                TabPreco = tabela
            };
            context.PrecoProduto.Add(precoProduto);
            context.SaveChanges();
        }
    }
    public void EditarProdutoTabela(int ProdutoId, double valor, int tabela)
    {
        using (var context = new indigo.DataModel.IndigoContext())
        {
            var precoProduto = context.PrecoProduto.Where(x => x.ProdutoId == ProdutoId && x.TabPrecoId == tabela).FirstOrDefault();
            precoProduto.preco = valor;
            context.PrecoProduto.Attach(precoProduto);
            var Entry = context.Entry(precoProduto);
            Entry.Property(e => e.preco).IsModified = true;
            context.SaveChanges();
        }
    }
    public void Remover(int id)
    {

    }

Please help me!

PS: Sorry for my bad english, I´m a Brazilian code student

UPDATE: My repository:

public List<PrecoProduto> ObterPrecoPorTabela(int copiarDe)
    {
        var Precos = new List<PrecoProduto>();
        using (var context = new indigo.DataModel.IndigoContext())
        {
            Precos = context.PrecoProduto.Where(pp => pp.TabPrecoId == copiarDe).ToList();
        }
        return Precos;
    }

Upvotes: 1

Views: 36

Answers (1)

KSib
KSib

Reputation: 911

On this line: TabPreco = tb, and then here Produto = item.Produto, I'm not positive you can do. You are taking an object that's correctly connected to your database context and trying to attach it to something that's no longer attached. Perhaps try removing those lines to see if it helps? I can't imagine why you'd need to both set those and set the IDs. You should only need to do one or the other.

Upvotes: 1

Related Questions