This question is locked. New answers and comments are not allowed.
Hi I need to use transaction to commit()/rollback() multiple database updates/inserts. I searched and found the following code:
IObjectScope scope = ObjectScopeProvider1.GetNewObjectScope();
ITransaction txn = scope.Transaction; But what is the ObjectScopeProvider1 object? Can someone give me a complete sample? Thanks in advance. |
4 Answers, 1 is accepted
0
Accepted
Hi Chen,
Ralph
the Telerik team
I guess you are working with the OpenAccessContext. The context manages transactions implicitly. Whenever you create an context, a transaction is started automatically. You can commit the transaction by calling SaveChanges() or rollback the transaction by calling ClearChanges(). Additionally you can find a FlushChanges() method on the context. You can find more information in our documentation.
Our SDK provides a lot examples on how to work with the context. This can be downloaded here.
The ObjectScopeProvider was used in older versions of the product. I recommend using the OpenAccessContext.
I hope this information is useful for you.
Do come back if you have any other question.
Ralph
the Telerik team
OpenAccess ORM Q2'12 Now Available! Get your hands on all the new stuff.
0
Chen
Top achievements
Rank 1
answered on 21 Jun 2012, 04:57 PM
Hi Ralph,
Thanks a lot for your help.
Per your reply, my understanding is that if I call SaveChanges() for a few times, then call ClearChanges(), it will roll back the changes done by the previouse SaveChanges() calls.
Am I right?
Chen
Thanks a lot for your help.
Per your reply, my understanding is that if I call SaveChanges() for a few times, then call ClearChanges(), it will roll back the changes done by the previouse SaveChanges() calls.
Am I right?
Chen
0
Hi Chen,
Calling SaveChanges() will do a commit on the database. So your assumption here is wrong. In order to send data to the server but not commit the data, you can use the FlushChanges() method. When you then call ClearChanges(), a rollback is executed. See the following snippet as example.
As you can see, in the end only obj3 is stored to the database. If you would replace FlushChanges() with SaveChanges() then all objects would be saved to the database.
Ralph
the Telerik team
Calling SaveChanges() will do a commit on the database. So your assumption here is wrong. In order to send data to the server but not commit the data, you can use the FlushChanges() method. When you then call ClearChanges(), a rollback is executed. See the following snippet as example.
using
(var ctx =
new
EntitiesModel())
{
var obj =
new
FlushClass(){ SomeValue =
"1"
};
ctx.Add(obj);
ctx.FlushChanges();
// data is sent to the database
var obj2 =
new
FlushClass(){ SomeValue =
"2"
};
ctx.Add(obj2);
ctx.FlushChanges();
// data is sent to the database
ctx.ClearChanges();
// a rollback is executed
var obj3 =
new
FlushClass() { SomeValue =
"3"
};
ctx.Add(obj3);
ctx.FlushChanges();
ctx.SaveChanges();
// only object 3 is stored
}
As you can see, in the end only obj3 is stored to the database. If you would replace FlushChanges() with SaveChanges() then all objects would be saved to the database.
Hope that helps.
All the best,Ralph
the Telerik team
OpenAccess ORM Q2'12 Now Available! Get your hands on all the new stuff.
0
Chen
Top achievements
Rank 1
answered on 22 Jun 2012, 07:16 PM
Ralph,
Thanks a lot for your clear sample. Now I think I fully understood it.
Chen
Thanks a lot for your clear sample. Now I think I fully understood it.
Chen