EntitySpaces™ Hierarchical Support
The EntitySpaces Hierarchical Template uses the foreign keys in your database
to automatically build the hierarchical object model. First the standard templates generate
the Collections and Entities then the Hierarchical Template generates a partial
class that hooks them all up in a hierarchical fashion.
EntitySpaces will automatically save down the hierarchical tree from Parent
to Child. This process is iterative, so calling save on an EntitySpaces
Northwind CustomersCollection (custs.Save()) will, also, save all changes to
custs.OrdersCollectionByCustomerID, which will in turn save any changes to
OrderDetailsCollectionByOrderID. If a Parent has an auto-incrementing key, then
that is retrieved and filled in to the foreign key column for you. Everything is
handled in the right order, so a Parent will not be deleted before the related
Children, or added after related Child records. This prevents foreign key
constraints from being violated in the database.
To prevent endless loops, EntitySpaces does not automatically save up the
hierarchical tree. To help you determine whether a particular hierarchical
sub-object is upstream or down, the default generated names for upstream
sub-objects will start with the prefix "UpTo".
Many-To-One
Let's start out by looking at a Many To One example using the Northwind Products
table which has a CategoryID that points to the product's category. The code below
loads a product, then accesses its category sub-object printing the name.
A many to one uses an Entity to represent the nested sub-object.
C#
VB
Zero-To-Many
A Zero To Many uses a collection to represent the nested sub-object. Here we are
going to loop through all the OrderDetails for a given Product. This example raises
the prices by 10%.
C#
VB
Now we are going to add an OrderDetails record to this Products data.
C#
VB
Many-To-Many
A Many-to-Many like the Zero-to-Many uses a collection to represent the nested sub-object. There
are two new methods on a Many-To-Many and they are Associate and Dissociate. EntitySpaces
implements the Many-to-Many in such a way that you never actually see the linking
table. However, there are EntitySpaces objects for the linking table, they are just
hidden away to make your life easier.
In this example we are going to loop through all of the Territories for a given
Employee. Behind the scenes there is set of EntitySpaces classes to represent the
EmployeeTerritories table however they are not exposed though the programmer can
use them at will whenever desired.
C#
VB
You can also associate and dissociate Territories as follows.
C#
VB
We hope this provides you an idea of how the EntitySpaces Hierarchical Model is
used.