Walaa wrote:
Removing an entity from a collection won't delete it from the database when you save the collection.
You's rather manually delete the entity.
Mmm... so it seems that the concept behind all this is that LLBLGen does not update collections i.e. propagates changes made to the collection (including new and removed elements) to the database, but rather uses collections to identify dependent entities and individually save these entities.
Then,
order.OrderItems.Add(item); // item becomes a dependent entity and will be saved ("added").
order.OrderItems.RemoveAt(3); // removed item is not a dependent entity anymore, full stop.
adapter.DeleteEntity(order.OrderItems[3]); // explicitly deletes ("removes") the item.
Correct?
To come back to my original point, which was about updating a many-to-many relation: assuming I have User -> Role (n:m via UserRole), and the UI lets me pick roles for a user by checking them in a list, and then gives me back a user ID and a list of role IDs. I would need to do something like:
void UpdateRoles(int userID, int[] roleIDs)
{
// should really wrap this in a transaction or a UnitOfWork
adapter.DeleteEntitiesDirectly("UserRoleEntity",
new RelationPredicateBucket(UserRoleFields.UserId == userID));
foreach (int roleID in roleIDs)
{
UserRoleEntity e = new UserRoleEntity();
e.UserId = userID;
e.RoleId = roleID;
adapter.SaveEntity(e);
}
}
Alternatively, I'd need to implement my own mechanism to track checked and unchecked roles, so I know which UserRoleEntity entities to selectively delete.
Do I make sense?