EntityCollection with mixed updates and adds

Posts   
 
    
bvalle
User
Posts: 54
Joined: 07-Jun-2006
# Posted on: 29-Sep-2006 15:17:22   

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

bvalle
User
Posts: 54
Joined: 07-Jun-2006
# Posted on: 29-Sep-2006 15:22:04   

Ops forgot the following information: LLBLGen Pro version: 1.0.2005.1 Final Released on: June 5th, 2006 .Net version 2.0.50727.42 Database: SQL 2005

Also my company has purchased LLBLGen Pro version: 2.0.0.0 Final, but we have not implemented yet for this piece of the system.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 29-Sep-2006 17:29:34   

You can make use of the IsNew property of the Entity classes, and store it in the XML file along other fields. And you will need to read it back into the Entity before attempting to save it (before saving the collection)

LLBLGen Pro automatically issue an Update statement or an Insert one according to the IsNew property.

bvalle
User
Posts: 54
Joined: 07-Jun-2006
# Posted on: 29-Sep-2006 19:27:57   

Thank you very much. The code is much nicer now.