Ronald
Ronald

Reputation: 667

Querying Azure TableServiceContext after adding object gives no result

In my application I am adding an entity to a TableServiceContext through the AddObject method. Later in the proces I want to query the TableServiceContext in order to retrieve this specific entity in order to update some properties, but the query doesn't give me a result. It will only give me a result if I do a SaveChanges immediately after the AddObject. This means that I have an extra roundtrip to the server. I would like to create and update the entity, and then call a SaveChanges to persist the entity to Azure Table Storage.

Does anyone know why I don't get a result when querying the context? Is there a way how to get the entity from the context without the extra call to SaveChanges?

Thanks

Ronald

Upvotes: 0

Views: 424

Answers (2)

dunnry
dunnry

Reputation: 6868

Sounds like you are trying to Upsert here. Have you seen the support for InsertOrReplace? There is also InsertOrMerge, but I think you are looking to overwrite.

http://msdn.microsoft.com/en-us/library/hh452242.aspx

Upvotes: 1

user94559
user94559

Reputation: 60153

AddObject just starts tracking the object in local memory, so it makes sense to me that querying table storage for it doesn't return it. (Table storage has no knowledge of it.) I'd say that if you want to do this, you should just keep track of the new entity yourself.

I'm a bit confused as to the scenario. It sounds like you want something like this (won't work):

var foo = new Foo();
context.AddObject("foo", foo);
...
foo = context.CreateQuery<Foo>("foo").Where(...).Single();
foo.bar = "baz";
context.UpdateObject(foo);
context.SaveChanges();

Why not replace with this?

var foo = new Foo();
...
foo.bar = "baz";
context.AddObject("foo", foo);

What about your code makes you have to go through the TableServiceContext to get at the object you created?

Upvotes: 0

Related Questions