0

first of all happy new year to all !!!!

i have following code to remove a record from the db table tr_shoppingCart

 tr_ShoppingCart cart = new tr_ShoppingCart
                        {
                            cartId = sc.CartId,
                            CartGenDt = sc.CreateTime,
                            UserId = ((SessionData)Session["object"]).sessObj.UserId,
                            prodId = obj.prodId,
                            Qnty = obj.Qnty,
                            Total = obj.Total
                        };


                        if (ModelState.IsValid)
                        {
                            //db.Entry(cart).State = System.Data.EntityState.Deleted;
                            db.tr_ShoppingCart.Attach(cart);
                            db.tr_ShoppingCart.Remove(cart);
                            db.SaveChanges();
                        }

but it has given the error:

An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.

enter image description here

here is my DB table tr_ShoppingCart

enter image description here

Phill Greggan
  • 1,524
  • 2
  • 15
  • 31

1 Answers1

0

This question is explained in more detail here. I assume that cartId is your key. Check that sc.CartId is not already in tr_ShoppingCart list. If it is, than you must ensure that you generate your ids correctly.

Community
  • 1
  • 1
Alexei - check Codidact
  • 17,850
  • 12
  • 118
  • 126