Hello
This thread is more of a check then a question. I just want to make sure I am approaching the problem correctly.
Basically I am readying an XML file and storing the entries into the correct entity collection, however this XML file could have updates and new entries for the database. If I try to only save the said collection I will get exceptions telling me the key has been duplicated and the record could not be added. Therefore I came up with the following logic:
1 – Read XML and stored on XmlEntityCollection
2 – Create a filter from all the keys of every record on the XML file
3 – Pulled another UpdateEntityCollection from the database using filter mention above
4 – Compared the items from the XmlEntityCollection against UpdateEntityCollection, if the item exists update the dirty fields into UpdateEntityCollection. If item does not exist add it to a third entity called AddEntityCollection.
5 – Save AddEntityCollection and UpdateEntityCollection.
Here is the code I have right now for the separation of the two EntityCollections.
private void SeparateUpdateAndAdd(ref EntityCollection newEntityCollection, ref EntityCollection updateEntityCollection, EntityCollection rawEntityCollection )
{
/// create the two collection that will be doing the actual database modifications
newEntityCollection = new EntityCollection(new AntennaTypeEntityFactory());
updateEntityCollection = new EntityCollection(new AntennaTypeEntityFactory());
IPredicateExpression filter = new PredicateExpression();
bool firstTime = true;
/// get all the keys and create a filter for it to pull all the matching records.
/// records that are found should be set for updates not add.
foreach (AntennaTypeEntity antennaTypeRecord in rawEntityCollection)
{
if (firstTime)
{
filter.Add(AntennaTypeFields.AntennaTypeID == antennaTypeRecord.AntennaTypeID);
firstTime = false;
}
else
{
filter.AddWithOr(AntennaTypeFields.AntennaTypeID == antennaTypeRecord.AntennaTypeID);
}
}
/// add the filter to the bucket to be processed
IRelationPredicateBucket bucket = new RelationPredicateBucket();
bucket.PredicateExpression.Add(filter);
/// pull all matching records
using (DataAccessAdapter dataAccessAdapter = new DataAccessAdapter(ConnectionString.GetCustomerInventory()))
{
dataAccessAdapter.FetchEntityCollection(updateEntityCollection, bucket);
}
/// now update the fields needing to be updated, and also create the list
/// of the new records
foreach (AntennaTypeEntity antennaTypeRawRecord in rawEntityCollection)
{
bool recordFound = false;
/// now compare the RAW data agains the found record
foreach (AntennaTypeEntity antennaTypeExistingRecord in updateEntityCollection)
{
/// is this the correct record
if (antennaTypeRawRecord.AntennaTypeID == antennaTypeExistingRecord.AntennaTypeID)
{
/// now to figure out what fields needs to be modified
for (int fieldNumber = 0; fieldNumber < antennaTypeRawRecord.Fields.Count; fieldNumber++)
{
if (antennaTypeRawRecord.Fields[fieldNumber].IsChanged &&
(!antennaTypeRawRecord.Fields[fieldNumber].IsPrimaryKey))
{
antennaTypeExistingRecord.Fields[fieldNumber].CurrentValue = antennaTypeRawRecord.Fields[fieldNumber].CurrentValue;
}
}
/// set the record as been found and get out of this loop
recordFound = true;
break;
}
}
/// do we add this record to the list to be added
if (!recordFound)
{
antennaTypeRawRecord.CountAvailable = 0;
antennaTypeRawRecord.CountOnOrder = 0;
antennaTypeRawRecord.CountDeployed = 0;
newEntityCollection.Add(antennaTypeRawRecord);
}
}
}
Am I correct on this logic? Or am I taking the long way to something that could be done easily?
Thank you in advance for your advise.
BValle