My code for fetching is something like this:
public abstract class ESGEntityBase : EntityBase2, ICloneable
{
private IEntityFields2 extraFields = null;
public IEntityFields2 ExtraFields
{
get { return extraFields; }
set { extraFields = value; }
}
................
}
public void FetchEntityCollection(IEntityCollection2 collectionToFill, IRelationPredicateBucket bucket, IEntityFields2 extraFields, int maxNumberOfItemsToReturn, ISortExpression sortClauses)
{
if (extraFields == null || extraFields.Count == 0)
{
this.FetchEntityCollection(collectionToFill, bucket, maxNumberOfItemsToReturn, sortClauses, null);
return;
}
IEntityFactory2 entityFactoryToUse = collectionToFill.EntityFactoryToUse;
IEntityFields2 entityFields = entityFactoryToUse.CreateFields();
IEntityFields2 allFields = new EntityFields2(entityFields.Count + extraFields.Count);
{
int i = 0;
for (; i < entityFields.Count; i++)
{
IEntityField2 field = entityFields[i];
allFields[i] = field;
}
for (int j=0; j < extraFields.Count; j++, i++)
{
IEntityField2 field = extraFields[j];
allFields[i] = field;
}
}
DataTable dataTable = new DataTable();
this.AdapterInternal.FetchTypedList(allFields, dataTable, bucket, maxNumberOfItemsToReturn, sortClauses, true);
for (int i = 0; i < dataTable.Rows.Count; ++i)
{
DataRow dataRow = dataTable.Rows[i];
ESGEntityBase newEntity = (ESGEntityBase) entityFactoryToUse.Create();
int columnIndex = 0;
for (int j = 0; j < newEntity.Fields.Count; ++j, ++columnIndex)
{
object valueInField = dataRow[columnIndex];
IEntityField2 fieldDestination = newEntity.Fields[j];
SetNewFieldValueFromRow(fieldDestination, valueInField);
}
newEntity.Fields.AcceptChanges();
IEntityFields2 newExtraFields = new EntityFields2(extraFields.Count);
for (int j = 0; j < extraFields.Count; ++j, ++columnIndex)
{
IEntityField2 extraField = (IEntityField2) ((ICloneable) extraFields[j]).Clone();
newExtraFields[j] = extraField;
object valueInField = dataRow[columnIndex];
SetNewFieldValueFromRow(extraField, valueInField);
}
newExtraFields.AcceptChanges();
newEntity.ExtraFields = newExtraFields;
collectionToFill.Add(newEntity);
}
}
private void SetNewFieldValueFromRow(IEntityField2 fieldDestination, object valueInField)
{
bool isColumnValueDBNull = (valueInField == DBNull.Value);
fieldDestination.IsNull = isColumnValueDBNull;
if (isColumnValueDBNull)
{
// Store default value for null for type
fieldDestination.ForcedCurrentValueWrite(this.AdapterInternal.TypeDefaultValueSupplier.DefaultValue(fieldDestination.DataType), null);
}
else
{
// simply store the value
fieldDestination.ForcedCurrentValueWrite(valueInField, valueInField);
}
}
Since those extra fields are not part of a normal entity, when they are changed in GUI they are not firing EntityContentsChanged event. Maybe I have to register them somewhere?