Skip to content

Instantly share code, notes, and snippets.

@sandrinodimattia
Created November 23, 2012 11:43
Show Gist options
  • Save sandrinodimattia/4135258 to your computer and use it in GitHub Desktop.
Save sandrinodimattia/4135258 to your computer and use it in GitHub Desktop.
UpdateObject
// Example 1: Simply updating an entity does not work if not calling UpdateObject
string tableName = "testupdate";
string partition = "Customers";
client.GetTableReference(tableName)
.CreateIfNotExists();
var rowKey = DateTime.Now.ToString("HHmmss");
// User A creates customer.
var contextUserA = new TableServiceContext(client);
contextUserA.AddObject(tableName, new SomeEntity()
{
Name = "Jack",
PartitionKey = partition,
RowKey = rowKey
});
contextUserA.SaveChanges();
// User B fetches customer Jack.
var contextUserB = new TableServiceContext(client);
var entityUserB = contextUserB.CreateQuery<SomeEntity>(tableName)
.Where(o => o.PartitionKey == partition && o.RowKey == rowKey)
.FirstOrDefault();
// User C fetches customer Jack.
var contextUserC = new TableServiceContext(client);
var entityUserC = contextUserC.CreateQuery<SomeEntity>(tableName)
.Where(o => o.PartitionKey == partition && o.RowKey == rowKey)
.FirstOrDefault();
// Update does not do anything since we didn't call UpdateObject.
entityUserB.Name = "Rambo";
contextUserB.SaveChanges(SaveChangesOptions.ReplaceOnUpdate);
entityUserC.Name = "Jesus";
contextUserC.SaveChanges(SaveChangesOptions.ReplaceOnUpdate);
// Example 2: Updating the entity works, but causes concurrency exception
string tableName = "testupdate";
string partition = "Customers";
client.GetTableReference(tableName)
.CreateIfNotExists();
var rowKey = DateTime.Now.ToString("HHmmss");
// User A creates customer.
var contextUserA = new TableServiceContext(client);
contextUserA.AddObject(tableName, new SomeEntity()
{
Name = "Jack",
PartitionKey = partition,
RowKey = rowKey
});
contextUserA.SaveChanges();
// User B fetches customer Jack.
var contextUserB = new TableServiceContext(client);
var entityUserB = contextUserB.CreateQuery<SomeEntity>(tableName)
.Where(o => o.PartitionKey == partition && o.RowKey == rowKey)
.FirstOrDefault();
// User C fetches customer Jack.
var contextUserC = new TableServiceContext(client);
var entityUserC = contextUserC.CreateQuery<SomeEntity>(tableName)
.Where(o => o.PartitionKey == partition && o.RowKey == rowKey)
.FirstOrDefault();
// Update works.
entityUserB.Name = "Rambo";
contextUserB.UpdateObject(entityUserB);
contextUserB.SaveChanges(SaveChangesOptions.ReplaceOnUpdate);
// This causes an exception, the ETag on the server is different since we updated the entity on the previous line.
entityUserC.Name = "Jesus";
contextUserC.UpdateObject(entityUserC);
contextUserC.SaveChanges(SaveChangesOptions.ReplaceOnUpdate);
// Example 3: Even though the ETag on the server is different, saving works (uncond. update)
string tableName = "testupdate";
string partition = "Customers";
client.GetTableReference(tableName)
.CreateIfNotExists();
var rowKey = DateTime.Now.ToString("HHmmss");
// User A creates customer.
var contextUserA = new TableServiceContext(client);
contextUserA.AddObject(tableName, new SomeEntity()
{
Name = "Jack",
PartitionKey = partition,
RowKey = rowKey
});
contextUserA.SaveChanges();
// User B fetches customer Jack.
var contextUserB = new TableServiceContext(client);
var entityUserB = contextUserB.CreateQuery<SomeEntity>(tableName)
.Where(o => o.PartitionKey == partition && o.RowKey == rowKey)
.FirstOrDefault();
// User C fetches customer Jack.
var contextUserC = new TableServiceContext(client);
var entityUserC = contextUserC.CreateQuery<SomeEntity>(tableName)
.Where(o => o.PartitionKey == partition && o.RowKey == rowKey)
.FirstOrDefault();
// Update works.
entityUserB.Name = "Rambo";
contextUserB.UpdateObject(entityUserB);
contextUserB.SaveChanges(SaveChangesOptions.ReplaceOnUpdate);
// No exception.
entityUserC.Name = "Jesus";
contextUserC.Detach(entityUserC);
contextUserC.AttachTo(tableName, entityUserC, "*");
contextUserC.UpdateObject(entityUserC);
contextUserC.SaveChanges(SaveChangesOptions.ReplaceOnUpdate);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment