0

I'm trying to delete an item in my application. This is how I try to do it in my button click event. First I check if the item exists in the database and then I proceed with deleting.

But when I try to delete, I get this error:

An entity object cannot be referenced by multiple instances of IEntityChangeTracker.

My code:

private void btnRemove_Click(object sender, EventArgs e)
{
    if (MessageBox.Show("Do you want to proceed with deleting?", "System Alert", MessageBoxButtons.YesNo) == DialogResult.Yes)
    {
        int jid = 0;
        int ProdLine = 0;
        int seritid = 0;

        if (dgvServices.SelectedRows.Count != 0)
        {
            DataGridViewRow row = this.dgvServices.SelectedRows[0];
            jid = Convert.ToInt32(row.Cells["JID"].Value.ToString());
            ProdLine = Convert.ToInt32(row.Cells["ProdLine"].Value.ToString());
            seritid = Convert.ToInt32(row.Cells["ServiceItem"].Value.ToString());
        }

        using (DataControllers.RIT_Allocation_Entities RAE = new DataControllers.RIT_Allocation_Entities())
        {
            jsmodel2 = RAE.Job_Service.Where(a => a.JID == jid && a.ProdLine == ProdLine && a.ServiceItem == seritid).OrderByDescending(x => x.ServiceItem) 
                                      .Take(1).FirstOrDefault();

            if (jsmodel2 == null)
            {
                // No service item exists for this
                // Nothing to delete
                MessageBox.Show("The item doesn't exist ","Alert");
                return;
            }
            else
            {
                // Delete
                using (DataControllers.RIT_Allocation_Entities RAEE = new DataControllers.RIT_Allocation_Entities())
                {
                    var entry = RAEE.Entry(jsmodel2);

                    if (entry.State == EntityState.Detached)
                    {
                        RAEE.Job_Service.Attach(jsmodel2);
                        RAEE.Job_Service.Remove(jsmodel2);
                        RAEE.SaveChanges();
                        populateServiceDetailsGrid();
                    }
                }
            }
        }                
    }          
}

How can I overcome this?

1
  • 1
    Use only one instance of your DataController because you have one instance of jsmodel2. Think about what the error is telling you. Commented Dec 9, 2018 at 12:58

1 Answer 1

2

Your entity is already tracked by RAE (thus the error), so you don't need a second DbContest. Simply replace:

// Delete
using (DataControllers.RIT_Allocation_Entities RAEE = new DataControllers.RIT_Allocation_Entities())
{
  var entry = RAEE.Entry(jsmodel2);

  if (entry.State == EntityState.Detached)
  {
    RAEE.Job_Service.Attach(jsmodel2);
    RAEE.Job_Service.Remove(jsmodel2);
    RAEE.SaveChanges();
    populateServiceDetailsGrid();
  }
}

with

 RAE.Job_Service.Remove(jsmodel2);
 RAE.SaveChanges();
 populateServiceDetailsGrid();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.