Advice on link table handling

Posts   
 
    
Posts: 28
Joined: 27-Mar-2007
# Posted on: 25-May-2007 18:46:23   

Hi,

I would like some tips on handling link tables. I am having some problems making sense of LLBLGen Pro and the way it handles entities in relation to related entries.  To demonstration the senario here is an example schema of the data

 Attribute <---- Property_Attribute ----> Property <---- PropertyTypeLink --->PropertyType

The property_Attribute and PropertyTypeLink are link tables and just contain a pair of foreign keys to convert M:N relationships to a pair of 1:M relationships. The Property entity also has a foreign key to the resort entity. A UI(Web page) has a grid view and details view. The details view has a drop down template field to convert the Property.resort_id to a resortname. Their are a set of standard Save, New and Delete Buttons at the bottom of the page.

I use separate LLBLGenPro2 datasources to feed the gridview, detailsview and resort dropdownlist. Live persistance is set to true for the gridview and resort drop down list, false for the detialsview. All the necessary event methods are set up including the Perform_Work for the details view.

The question is what approach would be best to Create,Update and Delete items and any associated link entiries.

I update the resort_id from the selectedvalue in the dropdown list prior to saving. The associated entries in the link tables are cleared the the new associations add entity by entity. I use DeleteDirect and a RelationPredicate which targets only the related entries e.g. For attributes linked to a property

            RelationPredicateBucket theRPB = new RelationPredicateBucket();
            theRPB.PredicateExpression.Add(new PredicateExpression(PropertyAttributeLinkFields.PropertyId == theProperty.PropertyId));
            da.DeleteEntitiesDirectly("PropertyAttributeLinkEntity", theRPB);

There must be a better way than hard coding the entity name.

I had a look at relations such as theProperty.PropertyTypeCollectionViaPropertyTypeLink This is a read only list of attributes and only appears to be useful for population the associations

I am using the current set of libraries and tools

[code]LBLGenPro version 2.0.0.0 Final Runtime Libs 2.0.0.0 DQE.Sql.Net.2.0 File version: 2.0.7.129 ORM.Support File version: 2.0.7..402[ Adapter DB interface/code]

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 26-May-2007 08:06:53   

Sorry, isn't clear what entities are you Adding/Updating/Deleting flushed : Attribute, Property_Attribute, Property, PropertyTypeLink or PropertyType? If is the Property Entity, for example, at the DetailsView are you adding attributes and links entities?

Code: RelationPredicateBucket theRPB = new RelationPredicateBucket(); theRPB.PredicateExpression.Add(new PredicateExpression(PropertyAttributeLinkFields.PropertyId == theProperty.PropertyId)); da.DeleteEntitiesDirectly("PropertyAttributeLinkEntity", theRPB);

There must be a better way than hard coding the entity name.

You could use the EntityName property of an existing entity to avoid hard coding the name.

David Elizondo | LLBLGen Support Team
Posts: 28
Joined: 27-Mar-2007
# Posted on: 29-May-2007 10:00:27   

hi David, Sorry if I wasn't clear. The web page allows both entities and entity associations to be added e.g. property entity and property_type_links and attribute type links ( Attribute and property type are not changed here.

For the entity name I assume you mean PropertyEntity.Property["EntityName"]. Is this correct?

Regards

Michael Mason

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 29-May-2007 11:02:03   

The question is what approach would be best to Create,Update and Delete items and any associated link entiries.

Deleting the Main Entity: -The easiest option is to set Cascade Delete in the Database, so the entries referncing the deleted entity would be automatically deleted from the link tables. -The other option is to handle it from code as you did, preferably using a Transaction.

Insert a Mian Entity with some Related Entities in An Intermediate Table (a little bit easier here): -Check the following question in the "How do I ...?" section of the manual: "How do I create a m:n relation between two entity objects?"

Updating the Main Entity: - If you are using in a stateless environment like a web application, then you might consider deleting all the intermediate entries related to the entity in hand, and then re-insert what you have in the Update method. - If you are maintaining a previously fetched collection of the related entities from the intermediate table, you might want to consider using a UOW object to hold the entities to be deleted and those to be inserted, as well as the saved main entity, and commit it in one go.