Problem with EntityFramework - An entity object can not be referenced by multiple instances of IEntityChangeTracker

0

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

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 it to create a record, to retrieve the item I use the following line:

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

When time saving 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 can not be referenced by multiple instances of IEntityChangeTracker

Does anyone have any suggestions?

    
asked by anonymous 15.05.2018 / 15:44

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 {     public partial class Platform: 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();
        }
    }
}

}

    
15.05.2018 / 18:50