I have two entities that are related by a m:n relationship. In the database I have 3 tables to store these entities:
- CheckDefinition (PK=CheckId)
- DataField (PK=DataFieldId)
- 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
}