Pregunta

This is the exception I get:

"An object with a key that matches the key of the supplied object could not be found in the ObjectStateManager. Verify that the key values of the supplied object match the key values of the object to which changes must be applied."

It is trown by the next line:

public void UpdateTransaction(Transaction transaction)
{
   db.Transactions.ApplyCurrentValues(transaction);
}

I read on this page that the error might becouse it is not attached, but when I try to attach it I get another exception:

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

By doing some reading that this means that the record is already attached to the model.

And when I try to detach it I get the next:

"The object cannot be detached because it is not attached to the ObjectStateManager."

This is my controller where I update the table "InputValue", now depending on the value I also want to make an update on the table "Transaction" wich is done by the updatedTransactions function.

    [HttpPost]
    public ActionResult Edit(int id, FormCollection collection)
    {
        var inputValue = inputValueRespository.GetInputValue(id);

        if (inputValue == null)
            return RedirectToAction("Index");

        try
        {
            UpdateModel(inputValue, collection.ToValueProvider());
            inputValue.InputValues_EditUser = WindowsIdentity.GetCurrent().Name;
            inputValue.InputValues_EditDate = DateTime.Today.Date;
            inputValueRespository.UpdateInputValue(inputValue);
            List<Transaction> updatedTransactions = inputValueRespository.HasTransactions(inputValue.Input_ID, inputValue.InputValues_Date, inputValue.InputValues_Week, inputValue.InputValues_Quarter);
            for (int i = 0; i < updatedTransactions.Count; i++)
            {

                transactionRepository.UpdateTransaction(updatedTransactions[i]);
            }
            inputValueRespository.save();
            return RedirectToAction("Create");
        }
        catch (Exception)
        {
            return View("Create");
        }

This is the function:

public List<Transaction> HasTransactions(int id, DateTime date, string week, string quarter)
    {
       // random code that decides if transaction should be update
         Transaction transaction = new Transaction();
            transaction = (from a in db.Transactions where a.KPI_ID == kpiId && a.Transaction_Period == period select a).SingleOrDefault();

// random code that generated new result

                // fill the list of transaction that will be returned         
                transaction.Transaction_Value = result;
                trasactions.Add(transaction);
            }
        }
        return trasactions;
    }

I'm completly clueless in this one, any help would be greatly appreciated.

PS: any other code where I use ApplyCurrentValue works perfectly.

¿Fue útil?

Solución

I finally got it working ( by trial and error )

In the end I need the detach the object, attach it, modify, apply and then save.

db.Transactions.Detach(transaction);
db.Transactions.Attach(transaction);
transaction.Transaction_Value = result;
db.Transactions.ApplyCurrentValues(transaction);
db.SaveChanges();

Does not make a lot of sense but I guess it is just the way it works :)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top