开发者

LINQ to SQL is not updating child element

开发者 https://www.devze.com 2023-04-12 17:18 出处:网络
I\'m grabbing a Cart object, which has a collection of CartItem objects. I have a function to add a CartItem object to the Cart object. If I simply call myCart.CartItems.Add(cartItem), it works fine.

I'm grabbing a Cart object, which has a collection of CartItem objects. I have a function to add a CartItem object to the Cart object. If I simply call myCart.CartItems.Add(cartItem), it works fine. In the event that the item already exists in the cart, I simply want to increment the Quantity field, rather than inserting a new record. This does not work. The existing CartItem object gets updated, but calling SubmitChanges on the ProjectDataContext does not save the change. Below is the code for my AddItem function.

public CartItem AddItem(CartItem cartItem)
{
    CartItem existingCartItem = this.CartItems.Where(c => c.DealOptionId == cartItem.DealOptionId&& c.isGift == cartItem.isGift).FirstOrDefault();

    if (existingCartItem != null)
    {
        existingCartItem.Quantity += cartItem.Quantity;
        return existingCartItem;
    }
    else
    {
        cartItem.CartId = this.CartId;

        if (cartItem.Price == 0)
        {
            ProjectDataContext pdc = Connection.GetContext();
            DealOption dealOption = pdc.DealOptions.SingleOrDefault(d => d.Id == cartItem.DealOptionId);
            cartItem.Price = dealOption.OfferPrice;
        }

        this.CartItems.Add(cartItem);
        return cartItem;
    }
}

The Cart object is attached to the ProjectDataContext properly, as it will add CartItem objects without a problem. It just will not update existing ones. I'm assuming it's just a trivial misunderstanding of LINQ to SQL on m开发者_StackOverflow社区y part.

Edit: More clarification.

That datacontext is only used for that one retrieval. The cart object has its own that works fine. Like I said, if I add a new object to the list, it works perfectly, but editing an existing one will not save to the DB. That's why I'm so confused.

I should also note that the additional ProjectDataContext below is never actually hit because of the if statement, so I know that's not interfering either.

Added

Here's the code that calls this function, including the ProjectDataContext

        ProjectDataContext pdc = Connection.GetContext();
        Model.Cart myCart = Model.Cart.GetCart(pdc);
        CartItem cartItem = new CartItem();
        DealOptionRepository dor = new DealOptionRepository(pdc);
        DealOption dealOption = dor.GetById(dealOptionId);
        cartItem.DealOptionId = dealOptionId;
        cartItem.DealId = dealOption.DealId;
        cartItem.Price = Convert.ToDecimal(hdnPrice.Value);

        cartItem.isGift = false;
        cartItem.Quantity = Convert.ToInt32(ddlBFMQty.SelectedValue);

        CartItem thisItem = myCart.AddItem(cartItem);
        pdc.SubmitChanges();


I was being stupid. I had a function in the Cart class to retrieve a cart based on id, and I forgot to remove the new DataContext in that function. Sorry for wasting everyone's time.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号