Using ManyToMany Relation with DataSource

Posts   
 
    
colincsb
User
Posts: 18
Joined: 01-Nov-2006
# Posted on: 29-Jan-2007 08:23:15   

I have two entities that are related by a m:n relationship. In the database I have 3 tables to store these entities:

  1. CheckDefinition (PK=CheckId)
  2. DataField (PK=DataFieldId)
  3. CheckData (mapping table containing CheckId, DataFieldId)

I am using the self-servicing model and corresponding entities CheckDefinitionEntity, DataFieldEntity, CheckDataEntity were created.

I 'unhid' the m:n relationship on both sides: CheckDefinitionEntity maps this to CheckDefinitionEntity.DataFields ... DataFieldEntity maps it to DataFieldEntity.CheckDefinitions ...

In my web page, I want to display two lists: 1. a list of all existing DataFields, and 2. the DataFields that are mapped to the CheckDefinition (CheckId is passed in the request parameters).

I have an LLBLGenProDataSource named DataFieldsDS with entity collection type DataFieldCollection bound to ListBox1 - This list box correctly lists all DataFields.

For the second list, I created another datasource named CheckDataFieldsDS with the same entity type and added a select parameter CheckId that is read from the request parameters.

This ListBox incorrectly listed ALL DataFields (same as first datasource with no select parameters!!)

I then turned off live persistence on the CheckDataFieldsDS and added the following code to the PeformSelect method:


        DataFieldCollection fields = new DataFieldCollection();
        if (Request.Params["checkId"] != null) {
            long checkId = long.Parse(Request.Params["checkId"]);
            CheckDefinitionEntity definition = new CheckDefinitionEntity(checkId);
            fields.GetMultiManyToManyUsingCheckDefinitions(definition);
        }
        CheckDataFieldsDS.EntityCollection = fields;

The second list then displayed correctly.

Question 1: Is there a way that I can get the CheckDataFieldsDS to use the m:n relation to select the data? I tried setting up the RelationsToUse value but the only relation on the DataFieldEntity is DataFieldEntity.Relations.CheckDataEntityUsingDataFieldId

Question 2: How do I correctly add a DataFieldEntity to the CheckDefinition.DataFields collection so that the relationship is persisted?

I tried the following code but no relationship was stored in the CheckData table:


                long addFieldId = long.Parse(ListBoxAvailableFields.SelectedValue);
                DataFieldEntity field = new DataFieldEntity(addFieldId);
                CheckDefinitionEntity check = new CheckDefinitionEntity(checkId);
                if (!field.IsNew && !check.DataFields.Contains(field)) {
                    check.DataFields.Add(field);
                    check.Save(true);
                    // Also tried check.DataFields.SaveMulti(); but also did not work
                }


Aurelien avatar
Aurelien
Support Team
Posts: 162
Joined: 28-Jun-2006
# Posted on: 29-Jan-2007 16:16:59   

Hi,

Question 1:

A solution should be to create a typed list based on DataFieldEntity and CheckDataEntity with all DataFieldEntity fields and CheckId from CheckDataEntity. With this typed list, the select parameter CheckId should work.

Question 2:

I'm not sure that manually adding field in check.DataFields collection is a good thing. I think you must use CheckData entity :


CheckDataEntity cd = new CheckDataEntity();
cd.DataField = field;
cd.CheckDefinition = check;
cd.Save(true);

colincsb
User
Posts: 18
Joined: 01-Nov-2006
# Posted on: 30-Jan-2007 00:58:00   

Thanks .. I have implemented this as you suggested - seems so easy once you know how :-)

All is working beautifully!!!!