bniemyjski posted on Friday, June 26, 2009
Hello,
I'm getting the above exception (
Invalid for root objects - use Delete instead), for an EditableChild called Cart.
[Serializable]
public partial class Cart : BusinessBase< Cart >
This EC is stored in an EditableChildList called CartList.
[Serializable]
public partial class CartList : BusinessListBase< CartList, Cart >
In a switchable object (Profile) I have a property declared:
private static readonly PropertyInfo<CartList> _shoppingCart = RegisterProperty<CartList>(p => p.ShoppingCart, Csla.RelationshipTypes.LazyLoad);
public CartList ShoppingCart
{
get
{
if (!FieldManager.FieldExists(_shoppingCart))
SetProperty(_shoppingCart, CartList.GetCart(UniqueID, true));
return GetProperty(_shoppingCart);
}
}
Inside of my CartList I have two methods:
/// <summary>
/// Add an item to the cart.
/// When ItemId to be added has already existed, this method will update the quantity instead.
/// </summary>
/// <param name="itemId">Item to add</param>
/// <param name="uniqueID">Cart's Unique ID</param>
/// <param name="isShoppingCart">Cart is a shopping cart.</param>
public void Add(string itemId, int uniqueID, bool isShoppingCart)
{
int index = 0;
bool found = false;
foreach (Cart cart in this)
{
if (cart.ItemId == itemId)
{
found = true;
break;
}
index++;
}
if (found)
Items[index].Quantity += 1;
else
{
Item item = Item.GetItem(itemId);
Product product = Product.GetProduct(item.ProductId);
Cart cart = Cart.NewCart();
cart.UniqueID = uniqueID;
cart.ItemId = itemId;
cart.Name = item.Name;
cart.ProductId = item.ProductId;
cart.IsShoppingCart = isShoppingCart;
cart.Price = item.ListPrice ?? item.UnitCost ?? 0;
cart.Type = product.Name;
cart.CategoryId = product.CategoryId;
Add(cart);
}
}
/// <summary>
/// Remove item from the cart based on itemId.
/// </summary>
/// <param name="itemId">ItemId of item to remove</param>
public void Remove(string itemId)
{
int index = 0;
foreach (Cart cart in this)
{
if (cart.ItemId == itemId)
break;
index++;
}
RemoveItem(index);
}
The Add works as expected but the last line in the remove method throws the exception when the collection is saved. I noticed that the child is never marked as a child object like it should be. Do you guys have any ideas? I have already tried
this.
Thanks
-Blake Niemyjski