MattAdamson wrote:
Can you elloborate further on why this wouldn't be possible to write into the framework?
If LLBLGen knows about the foreign keys it can surely work out how the entities relate to each other and therefore know the order of entities to remove first e.g. in this example delete the invoice lines before the invoice.
That's not always possible. If you have 2 or more paths from entity A to entity B, it can lead to inproper deletes. This is also the reason why SqlServer sometimes doesn't allow you to enable CASCADE delete on an FK. To quote from the Bol:
The series of cascading referential actions triggered by a single DELETE or UPDATE must form a tree containing no circular references. No table can appear more than once in the list of all cascading referential actions that result from the DELETE or UPDATE. The tree of cascading referential actions must not have more than one path to any given table. Any branch of the tree is terminated when it encounters a table for which NO ACTION has been specified or is the default.
LLBLGen Pro doesn't use a cached mapping model in-memory, it uses a distributed model, where entities know who they are and the system asks them what to do with them, but that's it. This also implies that the mapping information drives the code, not the underlying db model. this means that if you hide a relation, it's not used by the entities, but it IS there in the db.
To determine which entities to delete first, without having these entities in memory requires the complete db model in memory + it requires subqueries per level, and a graph can become widespread, leading to a lot of deletes and it then becomes important in which order the deletes take place which isn't really possible to specify in a proper way (as you would need to specify that for every PK entity type).
When all entities would be in memory, you could sort the graph in-memory and persist changes.
Because of this, we decided not to build it in. One advantage of not having a mapping file with mapping info is that the code itself contains all information. One could use attributes but then adapter would be difficult. One other advantage is that you can decide per situation what to do: set FK's to null or remove related entities.
You can probably guess that I'm trying to write similar code right now
Utilize the feature to delete entities directly. For example, if you look at the code of HnD, where a forum is deleted for example, it simply removes all info in the right order.
Some say this is a leaky abstraction, as it leaks the referential integrity into the code, however I find that a bit silly: your entities do have references as well, which means that they thus have a 'depending - dependent' relationship, no matter how hard one tries to deny that. To be able to remove an entity on which others depend on, it's key to remove / reset these first, be it in your own code or be it in the db, because if you don't your data in-memory will be in an undefined state during the actions.
We've always been pretty careful with deletes: they're never automatic: you have to tell the code what to delete.