sausage_mouse
sausage_mouse

Reputation: 142

How to make several methods one transaction in asp.net MVC using EF

I want to execute 2 Actions and make them atomic. This is what I want to achieve:

public IActionResult DataUserForm()
{
    return View();
}

[HttpPost]
public IActionResult Handler(data)
{
    /*
        Do something with data
    */
    return RedirectToAction("Index");
}

DataUserForm View sends a Form to User where he can write some data. Then he is redirected to Handler, that does some business logic and redirects to Index homePage. I want to make all this Actions a transaction

There is already BeginTransaction() method in EF. But it can only be used with specific block of code and not in the methods. I also can not pass transaction as a parameter to action, because there are some problems with submit buttons and c# object

Upvotes: 0

Views: 255

Answers (1)

David Browne - Microsoft
David Browne - Microsoft

Reputation: 89091

Your DbContext is a scoped service, so you have a single instance per web request.

Upvotes: 1

Related Questions