TransactionAttribute Class
.NET Standard 2.x
Specifies that an automatically manage transaction need to be used surrounds execution of the action and the action result.
The Transaction
attributes can be applied to the controller class or the action methods in it.
Namespace: SnapObjects.Data.AspNetCore
Assembly: SnapObjects.Data.AspNetCore.dll
Syntax
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
public class TransactionAttribute : ActionFilterAttribute
Constructors
Name | Description |
---|---|
TransactionAttribute() | Initializes a new Instance of the TransactionAttribute class. |
Properties
Name | Return Type | Description |
---|---|---|
AsyncFlowOption | TransactionScopeAsyncFlowOption | Gets or sets a TransactionScopeAsyncFlowOption enumeration that describes whether the ambient transaction associated with the transaction scope will flow across thread continuations when using Task or async/await .NET async programming patterns. |
CommitPolicy | CommitPolicy | Gets or sets when to commit the transaction. |
IsolationLevel | IsolationLevel | Gets or sets the isolation level of transaction. |
ScopeTimeOut | double | Gets or sets the time (in milliseconds) after which the transaction scope times out and aborts the transaction. |
Methods
Name | Return Type | Description |
---|---|---|
OnActionExecuted | void | Called by the ASP.NET MVC framework after the action method is executed. |
OnActionExecuting | void | Called by the ASP.NET MVC framework before the action method is executed. |
OnResultExecuted | void | Called by the ASP.NET MVC framework after the action result is executed. |
Examples
The following code demonstrates how to use the Transaction
Attribute in an action of the controller to automatically manage transactions.
using Appeon.ApiDoc.Models.School;
using Microsoft.AspNetCore.Mvc;
using SnapObjects.Data;
using SnapObjects.Data.AspNetCore;
using System.Collections.Generic;
using System.Transactions;
namespace Appeon.ApiDoc.TransactionAttributeExamples
{
public class Action_TransactionExample : ControllerBase
{
SchoolContext _dataContext;
public Action_TransactionExample(SchoolContext dataContext)
{
// Sets the data context.
_dataContext = dataContext;
}
// The time out period for the transaction is set to 1000 ms;
// The isolation level of the transaction is set to Serializable;
// The transaction flow across thread continuations is suppressed;
// It commits the transaction after the action result executes.
[Route("examples/WebExtensions/[action]")]
[HttpPost]
[Transaction(ScopeTimeout = 1000,
IsolationLevel = IsolationLevel.Serializable,
AsyncFlowOption = TransactionScopeAsyncFlowOption.Suppress,
CommitPolicy = CommitPolicy.OnResultExecuted)]
public string Example(IList<Person> persons)
{
var mapper = _dataContext.SqlModelMapper;
mapper.TrackCreateRange(persons);
// It save changes using the transaction created by the `Transaction`
// Attribute.
mapper.SaveChanges();
return "SUCCESS";
}
}
}
Applies to
.NET Standard
2.x