Best way to update a Many-Many table?

Posts   
 
    
andyste1
User
Posts: 1
Joined: 03-Oct-2007
# Posted on: 03-Oct-2007 23:41:08   

Hi

I have entities "Hotel" and "Feature" (e.g. swimming pool, bar, etc.), and there is also a "HotelFeature" entity which is the M-M join between them. This entity simply contains the Hotel ID and Feature ID.

I have an ASP.Net page where a user can edit details of a Hotel, including a checkbox list where they choose the hotel's features. When the page first loads I retrieve the Hotel entity and its features, and use this to populate the webform, but I don't persist the entities (to avoid increasing viewstate size), I only persist the Hotel entity's ID (used during the save). BTW I am using the adapter pattern.

When the user clicks "Save", I create a new Hotel entity, populate its details including the ID I persisted earlier, and set IsNew=False. This will save the Hotel but what's the best way to deal with the HotelFeature entities? LLBLGen needs to know which ones are to be added, updated or deleted from persistent storage. Is the easiest approach just to delete all HotelFeature entities in persistent storage prior to saving the Hotel and its new HotelFeatures? Is there a slicker way of doing this, bearing in mind I'm not persisting the HotelFeature entity collection (unless I perform another hit prior to saving to retrieve them)?

Thanks in advance

Andy

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 04-Oct-2007 03:14:59   

Hi Andy,

I that case (you dont include the HotelFeatures graph in Viewstate) if impossible to figure out what values are for update, insert or delete.

In that case you have 2 opctions:

  1. Perform a Direct Delete on the HotelFeature collection filtered by HotelId. Then insert all active HotelFeature entities selected.

  2. Retrieve the HotelFeature collection prior to save the new one. Then compare each entity to see wich is added, deleted or updated. At this point would be helpul read LLBLGenPro Help - Using generated code - (Adpater) - Using Context object.

Is passing the HotelFeature graph to Viewstate too much expensive?

David Elizondo | LLBLGen Support Team