Entityframework problem - An Entity Object cannot be referenced by Multiple instances of Ientitychangetracker

Asked

Viewed 146 times

0

I have a project where a dropdownlist is loaded with the following instructions:

var lista = entities.prato.OrderBy(x => x.nome).ToList();

combo.DataTextField = "nome";
combo.DataValueField = "id";
combo.DataSource = lista;
combo.DataBind();

Then I need to retrieve the item from dropdownlist and use for creating a record, to retrieve the item I use the following line:

ingrediente i = entities.ingrediente.Find (Convert.ToInt32(combo.SelectedItem.Value)); 

When time save the record as follows:

ingrediente_prato ip = new ingrediente_prato();
ip.prato = (prato)Session["pratoSelecionado"];
ip.ingrediente = i;
entities.ingrediente_prato.Add(ip);
entities.SaveChanges();

the following error is returned:

An Entity Object cannot be referenced by Multiple instances of Ientitychangetracker

Does anyone have any suggestions?

  • You have to post the entire code !!! the classes also may not even need to retrieve the ingredient!

1 answer

0

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.Webcontrols;

namespace Refeitorio { public partial class Dish : System.Web.UI.Page {

    private bancodadosEntities entities = new bancodadosEntities();
    private List<ingrediente> ingredientes;



    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) {
            carregarGrid();
            carregaListIngredientes();
            entities.ingrediente_prato.OrderBy(x => x.ingrediente.nome).ToList();
        }
    }

    private void limpar()
    {
        txtID.Text = string.Empty;
        txtNome.Text = string.Empty;
    }

    private void carregarGrid()
    {
        var lista = entities.prato.OrderBy(x => x.nome).ToList();
        grid.DataSource = lista;
        grid.DataBind();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        limpar();
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        if (txtID.Text == string.Empty)
        {
            prato p = new prato();
            p.nome = txtNome.Text;
            entities.prato.Add(p);
            entities.SaveChanges();
            limpar();
            carregarGrid();
        }
    }

    private void carregaListIngredientes()
    {
        var lista = entities.prato.OrderBy(x => x.nome).ToList();

        var lista2 = entities.prato.OrderBy(x => x.nome).ToList();
        combo.DataTextField = "nome";
        combo.DataValueField = "id";
        combo.DataSource = lista;
        combo.DataBind();

    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        var a = combo.Items.FindByValue(combo.SelectedValue);

        ingrediente i = entities.ingrediente.Find (Convert.ToInt32(combo.SelectedItem.Value));

        entities.Entry(i);
        ingrediente_prato ip = new ingrediente_prato();
        ip.prato = (prato)Session["pratoSelecionado"];
        ip.ingrediente = i;
        ip.quantidade = Convert.ToInt32(txtQtdIngredientes.Text);
        entities.ingrediente_prato.Add(ip);
        entities.SaveChanges();
        carregaListIngredientes();
        combo.SelectedValue = null;
        txtQtdIngredientes.Text = string.Empty;
    }

    private void carregarGridIngredientes()
    {
        gridIngredientes.DataSource = ingredientes;
        gridIngredientes.DataBind();
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("Ingredientes"))
        {
            int linha = Convert.ToInt32(e.CommandArgument.ToString());
            var value = Convert.ToInt32(grid.Rows[linha].Cells[0].Text.ToString());
            Session["pratoSelecionado"] = entities.prato.Find(value);
            ModalPopupExtender1.Show();
        }
    }
}

}

Browser other questions tagged

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