Direct update of entity via Sql Server UniquIdentifier field..

Posts   
 
    
DarinSh
User
Posts: 9
Joined: 25-Feb-2007
# Posted on: 26-Apr-2007 01:08:28   

1.0.2005.1, Self Servicing. I understand how to directly update an entity without first having to fetch that entity via setting the .IsNew property to false and setting the primary key value via the ForcedCurrentValueWrite method. I would like to do the same thing without knowing the primary key field's value, instead only knowing the value of a SQL Server UniqueIdentifier (guid) field.

The reason for this is that I do not expose the primary key field values to the web services layer. I use a guid at that layer to uniquely identify entities.

I tried this


TransactionItemEntity tie = new TransactionItemEntity();

tie.IsNew = false;
tie.Guid = TransactionItemGuid;       // this is the unique identifier field.
tie.UserCustomField1 = "whatever";

tie.Save();

but no luck.

I know I can first fetch the entity via the guid, but for performance reasons I dont want to make an unecessary call to the database.

Is this possible?

Thanks!

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 26-Apr-2007 09:21:25   

Hi Darin, you can use EntityCollection.UpdateMulti(newValues, filter). Ref: LLBLGenPro Help - SelfServicing - Using the entity collection classes - Entity data manipulation using collection classes - Updating a set of entities in the persistent storage. In your case, only one row will be updated.

// coll to update
TransactionItemCollection ties = new TransactionItemCollection ();

// setting new update values
TransactionItemEntity newUpdateValues = new TransactionItemEntity ();
newUpdateValues .UserCustomField1 =  TransactionItemGuid;

// filter the entities (rows) that will be updated
IPredicateExpression selectFilter = new PredicateExpression();
selectFilter.AddWithAnd(
   new FieldCompareValuePredicate(TransactionItemFieldIndex.Guid, ComparisonOperator.Equal,  TransactionItemGuid));

// perform update
int amountRowsAffected = ties.UpdateMulti(newUpdateValues , selectFilter);
David Elizondo | LLBLGen Support Team
DarinSh
User
Posts: 9
Joined: 25-Feb-2007
# Posted on: 26-Apr-2007 18:44:37   

Alright, thanks man. I actually knew that, but wasnt thinking of it as the other method took me down the wrong path.

Nice work!