Database ExtensionsOpen AsyncT Method
This extension method is used to open a connection on a data context asynchronously when constructed.
Definition
Namespace: EWSoftware.EntityFramework
Assembly: EWSoftware.EntityFramework (in EWSoftware.EntityFramework.dll) Version: 2025.11.12.0
The passed data context object
Assembly: EWSoftware.EntityFramework (in EWSoftware.EntityFramework.dll) Version: 2025.11.12.0
C#
public static Task<T> OpenAsync<T>(
this T dataContext,
CancellationToken cancellationToken = default
)
where T : DbContext
VB
<ExtensionAttribute>
Public Shared Function OpenAsync(Of T As DbContext) (
dataContext As T,
Optional cancellationToken As CancellationToken = Nothing
) As Task(Of T)C++
public:
[ExtensionAttribute]
generic<typename T>
where T : DbContext
static Task<T>^ OpenAsync(
T dataContext,
CancellationToken cancellationToken = CancellationToken()
)F#
[<ExtensionAttribute>]
static member OpenAsync :
dataContext : 'T *
?cancellationToken : CancellationToken
(* Defaults:
let _cancellationToken = defaultArg cancellationToken new CancellationToken()
*)
-> Task<'T> when 'T : DbContextParameters
- dataContext T
- The data context on which to open the connection.
- cancellationToken CancellationToken (Optional)
- An optional cancellation token
Type Parameters
- T
- The data context type
Return Value
TaskTThe passed data context object
Usage Note
In Visual Basic and C#, you can call this method as an instance method on any object of type T. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).Remarks
This method can be used in conjunction with the NoTrackingT(T) extension method
as shown in the example below.
Example
C#
using var dataContext = await new MyDbContext().OpenAsync();
... Execute commands ...
using var dataContext = await new MyDbContext().NoTracking().OpenAsync();
... Execute commands ...