protected void DoDelete(ref Cart item) { // If we're not dirty then don't update the database. if (!item.IsDirty) return; // If we're new then don't call delete. if (item.IsNew) return; var criteria = new CartCriteria{CartId = item.CartId}; DoDelete(criteria); MarkNew(item); }
//Associations.Where(a => a.AssociationType == AssociationType.ManyToOne || a.AssociationType == AssociationType.ManyToZeroOrOne) private static void Update_Profile_Profile_FK_Cart_Profiles(ref Cart item) { item.Profile.UniqueID = item.UniqueID; new ProfileFactory().Update(item.Profile, true); }
private void DoUpdate(ref Cart item, bool stopProccessingChildren) { bool cancel = false; OnUpdating(ref cancel); if (cancel) return; // Don't update if the item isn't dirty. if (item.IsDirty) { const string commandText = "UPDATE [dbo].[Cart] SET [UniqueID] = @p_UniqueID, [ItemId] = @p_ItemId, [Name] = @p_Name, [Type] = @p_Type, [Price] = @p_Price, [CategoryId] = @p_CategoryId, [ProductId] = @p_ProductId, [IsShoppingCart] = @p_IsShoppingCart, [Quantity] = @p_Quantity WHERE [CartId] = @p_CartId"; using (var connection = new SqlConnection(ADOHelper.ConnectionString)) { connection.Open(); using(var command = new SqlCommand(commandText, connection)) { command.Parameters.AddWithValue("@p_CartId", item.CartId); command.Parameters.AddWithValue("@p_UniqueID", item.UniqueID); command.Parameters.AddWithValue("@p_ItemId", item.ItemId); command.Parameters.AddWithValue("@p_Name", item.Name); command.Parameters.AddWithValue("@p_Type", item.Type); command.Parameters.AddWithValue("@p_Price", item.Price); command.Parameters.AddWithValue("@p_CategoryId", item.CategoryId); command.Parameters.AddWithValue("@p_ProductId", item.ProductId); command.Parameters.AddWithValue("@p_IsShoppingCart", item.IsShoppingCart); command.Parameters.AddWithValue("@p_Quantity", item.Quantity); using(var reader = new SafeDataReader(command.ExecuteReader())) { //RecordsAffected: The number of rows changed, inserted, or deleted. -1 for select statements; 0 if no rows were affected, or the statement failed. if(reader.RecordsAffected == 0) throw new DBConcurrencyException("The entity is out of date on the client. Please update the entity and try again. This could also be thrown if the sql statement failed to execute."); if(reader.Read()) { item.CartId = reader.GetInt32("CartId"); } } } } } MarkOld(item); CheckRules(item); if(!stopProccessingChildren) { // Update Child Items. Update_Profile_Profile_FK_Cart_Profiles(ref item); } OnUpdated(); }
public Cart Update(Cart item, bool stopProccessingChildren) { if(item.IsDeleted) { DoDelete(ref item); MarkNew(item); } else if(item.IsNew) { DoInsert(ref item, stopProccessingChildren); } else { DoUpdate(ref item, stopProccessingChildren); } return item; }
public Cart Update(Cart item) { return Update(item, false); }
private void DoInsert(ref Cart item, bool stopProccessingChildren) { // Don't update if the item isn't dirty. if (!item.IsDirty) return; bool cancel = false; OnInserting(ref cancel); if (cancel) return; const string commandText = "INSERT INTO [dbo].[Cart] ([UniqueID], [ItemId], [Name], [Type], [Price], [CategoryId], [ProductId], [IsShoppingCart], [Quantity]) VALUES (@p_UniqueID, @p_ItemId, @p_Name, @p_Type, @p_Price, @p_CategoryId, @p_ProductId, @p_IsShoppingCart, @p_Quantity); SELECT [CartId] FROM [dbo].[Cart] WHERE CartId = SCOPE_IDENTITY()"; using (var connection = new SqlConnection(ADOHelper.ConnectionString)) { connection.Open(); using(var command = new SqlCommand(commandText, connection)) { command.Parameters.AddWithValue("@p_UniqueID", item.UniqueID); command.Parameters.AddWithValue("@p_ItemId", item.ItemId); command.Parameters.AddWithValue("@p_Name", item.Name); command.Parameters.AddWithValue("@p_Type", item.Type); command.Parameters.AddWithValue("@p_Price", item.Price); command.Parameters.AddWithValue("@p_CategoryId", item.CategoryId); command.Parameters.AddWithValue("@p_ProductId", item.ProductId); command.Parameters.AddWithValue("@p_IsShoppingCart", item.IsShoppingCart); command.Parameters.AddWithValue("@p_Quantity", item.Quantity); using(var reader = new SafeDataReader(command.ExecuteReader())) { if(reader.Read()) { item.CartId = reader.GetInt32("CartId"); } } } } MarkOld(item); CheckRules(item); if(!stopProccessingChildren) { // Update Child Items. Update_Profile_Profile_FK_Cart_Profiles(ref item); } OnInserted(); }
partial void OnAddNewCore(ref Cart item, ref bool cancel);