0

I have a little problem. I am encountering the following error each time I try update my entityset.

Unable to update the EntitySet 'ShoppingCart' because it has a DefiningQuery and no InsertFunction element exists in the ModificationFunctionMapping element to support the current operation.

The code is: `

public void AddItem(string cartID, string productID, string quantity)
        {
            using (CommerceEntities db = new CommerceEntities())
            {
                try
                {
                    var myItem = (from c in db.ShoppingCarts
                                  where c.CartID == cartID &&
                                      c.ProductID == productID
                                  select c).FirstOrDefault();
                    if (myItem == null)
                    {
                        ShoppingCart cartadd = new ShoppingCart();
                        cartadd.CartID = cartID;
                        cartadd.Quantity = quantity;
                        cartadd.ProductID = productID;
                        cartadd.DateCreated = DateTime.Now;
                        db.ShoppingCarts.AddObject(cartadd);
                    }
                    else
                    {
                        myItem.Quantity += Convert.ToInt32(quantity);
                    }
                    db.SaveChanges();
                }
                catch (Exception exp)
                {
                    throw new Exception("ERROR: Unable to Add Item to Cart - " +exp.Message);
                }
            }
        }

`

Please help me. I can provide more information if required, I am new to this Entity Framework Model and following the tutorial on This page.

Update: I added primary keys and redesigned the whole database. Now the error changed to:

System.Data.UpdateException was unhandled by user code Message=An error occurred while updating the entries. See the inner exception for details.

Samarth Agarwal
  • 2,044
  • 8
  • 39
  • 79

1 Answers1

0

This usually happens if entity set is mapped from database view, custom database query or if database table doesn't have primary key.

Karthik Chintala
  • 5,465
  • 5
  • 30
  • 60