If you have a database model like:
and you want to edit a CustomerEntity with LLGLGen Pro, before you save the CustomerEntity, you should delete all records in the table CustomerPermission.
Update a Customer
The code is from a ASP .NET Webpage that uses a DetailsView to edit a Customer. It uses a CheckBoxList in the DetailsView to add and delete references between the table Customer and the table Permission.
public void Update_Click(object sender, DetailsViewUpdateEventArgs e) { // Get the current CustomerId from the current URL string customerIdInQueryString = Request.QueryString["CustomerId"]; if (!string.IsNullOrEmpty(customerIdInQueryString)) { // Only save or create a customer when a CustomerId is supplied in the URL int customerId = 0; if (int.TryParse(customerIdInQueryString, out customerId)) { CustomerEntity customer = new CustomerEntity(); // If customerId == -1 create a new customer, else edit an existing customer if (customerId == -1) { customer.IsNew = true; } else { customer.IsNew = false; } // Set customer properties customer.Id = customerId; customer .Name = e.NewValues["Name"] == null ? null : e.NewValues["Naam"].ToString();
// Foreach item in the CheckBoxList that is selected create a reference from Customer to Permission CheckBoxList cbl = (CheckBoxList)this.customerDetailsView.FindControl("PermissionCheckBoxList"); customer.CustomerPermission.Clear(); foreach (ListItem li in cbl.Items) { if (li.Selected) { CustomerPermissionEntity spe = new CustomerPermissionEntity() { CustomerId = customer.Id, PermissionId = int.Parse(li.Value) }; customer.CustomerPermission.Add(spe); } } this.UpdateCustomer(customer); // Go back to overview page (containing a RadGrid) Response.Redirect("CustomerOverview.aspx"); } } }
private void UpdateCustomer(Customerntity customer) { using (var adapter = new DataAccessAdapter(ConfigurationManager.ConnectionStrings["Main.ConnectionString"].ConnectionString)) { // Delete entries in table CustomerPermission RelationPredicateBucket bucket = new RelationPredicateBucket(); bucket.PredicateExpression.Add(CustomerPermissionFields.CustomerId == customer.Id); adapter.DeleteEntitiesDirectly(typeof(CustomePermissionEntity), bucket); // Save Customer adapter.SaveEntity(customer, true, true); adapter.CloseConnection(); } }