Continue to explore for friends who want to use the Entity Framework. Although the content they share is not technical, it has been tested in practice.
It is easy to use transactions in Entity Framework. Put operations in transactionscope and submit transactions using the complete () method.
ExampleCodeAs follows:
Using (Blogdbcontext Context = New Blogdbcontext ())
{
Using (Transactionscope transaction = New Transactionscope ())
{
Context. blogposts. Add (blogpost );
Context. savechanges ();
Postbody. ID = Blogpost. ID;
Context. entryviewcounts. Add (
New Entryviewcount () {entryid = Blogpost. ID });
Context. postbodys. Add (postbody );
Context. savechanges ();
// Commit transactions
Transaction. Complete ();
}
}
After testing and verification, as long as an exception occurs in the code before transaction. Complete (), the transaction will be rolled back.
Continue to explore for friends who want to use the Entity Framework. Although the content they share is not technical, it has been tested in practice.
It is easy to use transactions in Entity Framework. Put operations in transactionscope and submit transactions using the complete () method.
The sample code is as follows:
Using (Blogdbcontext Context = New Blogdbcontext ())
{
Using (Transactionscope transaction = New Transactionscope ())
{
Context. blogposts. Add (blogpost );
Context. savechanges ();
Postbody. ID = Blogpost. ID;
Context. entryviewcounts. Add (
New Entryviewcount () {entryid = Blogpost. ID });
Context. postbodys. Add (postbody );
Context. savechanges ();
// Commit transactions
Transaction. Complete ();
}
}
After testing and verification, as long as an exception occurs in the code before transaction. Complete (), the transaction will be rolled back.