Added DbExtensions for database transactions
This commit is contained in:
@@ -114,7 +114,7 @@ namespace Microsoft.EntityFrameworkCore
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static DatabaseProvider GetProviderType(this DatabaseFacade database)
|
internal static DatabaseProvider GetProviderType(this DatabaseFacade database)
|
||||||
=> GetProviderType(database.ProviderName);
|
=> GetProviderType(database.ProviderName);
|
||||||
|
|
||||||
private static DatabaseProvider GetProviderType(this DbConnection connection)
|
private static DatabaseProvider GetProviderType(this DbConnection connection)
|
||||||
@@ -342,7 +342,7 @@ END;"
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private enum DatabaseProvider
|
internal enum DatabaseProvider
|
||||||
{
|
{
|
||||||
MySQL = 1,
|
MySQL = 1,
|
||||||
Oracle = 2,
|
Oracle = 2,
|
||||||
|
|||||||
@@ -0,0 +1,91 @@
|
|||||||
|
using System;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.EntityFrameworkCore.Storage;
|
||||||
|
|
||||||
|
namespace Microsoft.EntityFrameworkCore
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Extensions for the <see cref="DbContext"/>.
|
||||||
|
/// </summary>
|
||||||
|
public static class DbContextExensions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Starts a new transaction.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// See <see href="https://aka.ms/efcore-docs-transactions">Transactions in EF Core</see> for more information.
|
||||||
|
/// </remarks>
|
||||||
|
/// <param name="dbContext">The current <see cref="DbContext"/>.</param>
|
||||||
|
/// <returns>
|
||||||
|
/// A <see cref="IDbContextTransaction" /> that represents the started transaction.
|
||||||
|
/// </returns>
|
||||||
|
public static IDbContextTransaction BeginTransaction(this DbContext dbContext)
|
||||||
|
{
|
||||||
|
if (dbContext.Database.GetProviderType() == DatabaseFacadeExtensions.DatabaseProvider.InMemory)
|
||||||
|
return new DbContextTransactionStub();
|
||||||
|
|
||||||
|
return dbContext.Database.BeginTransaction();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously starts a new transaction.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// <para>
|
||||||
|
/// Entity Framework Core does not support multiple parallel operations being run on the same DbContext instance. This
|
||||||
|
/// includes both parallel execution of async queries and any explicit concurrent use from multiple threads.
|
||||||
|
/// Therefore, always await async calls immediately, or use separate DbContext instances for operations that execute
|
||||||
|
/// in parallel. See <see href="https://aka.ms/efcore-docs-threading">Avoiding DbContext threading issues</see>
|
||||||
|
/// for more information.
|
||||||
|
/// </para>
|
||||||
|
/// <para>
|
||||||
|
/// See <see href="https://aka.ms/efcore-docs-transactions">Transactions in EF Core</see> for more information.
|
||||||
|
/// </para>
|
||||||
|
/// </remarks>
|
||||||
|
/// <param name="dbContext">The current <see cref="DbContext"/>.</param>
|
||||||
|
/// <param name="cancellationToken">A <see cref="CancellationToken" /> to observe while waiting for the task to complete.</param>
|
||||||
|
/// <returns>
|
||||||
|
/// A task that represents the asynchronous transaction initialization. The task result contains a <see cref="IDbContextTransaction" /> that represents the started transaction.
|
||||||
|
/// </returns>
|
||||||
|
/// <exception cref="OperationCanceledException">If the <see cref="CancellationToken" /> is canceled.</exception>
|
||||||
|
public static Task<IDbContextTransaction> BeginTransactionAsync(this DbContext dbContext, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
if (dbContext.Database.GetProviderType() == DatabaseFacadeExtensions.DatabaseProvider.InMemory)
|
||||||
|
return Task.FromResult<IDbContextTransaction>(new DbContextTransactionStub());
|
||||||
|
|
||||||
|
return dbContext.Database.BeginTransactionAsync(cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IDbContextTransaction" />
|
||||||
|
private class DbContextTransactionStub : IDbContextTransaction
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
public Guid TransactionId { get; private set; } = Guid.NewGuid();
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public void Commit()
|
||||||
|
{ }
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public Task CommitAsync(CancellationToken cancellationToken = default)
|
||||||
|
=> Task.CompletedTask;
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public void Dispose()
|
||||||
|
{ }
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public ValueTask DisposeAsync()
|
||||||
|
=> new(Task.CompletedTask);
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public void Rollback()
|
||||||
|
{ }
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public Task RollbackAsync(CancellationToken cancellationToken = default)
|
||||||
|
=> Task.CompletedTask;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -14,9 +14,9 @@ namespace AMWD.Common.Logging
|
|||||||
/// Implements a file logging based on the <see cref="ILogger"/> interface.
|
/// Implements a file logging based on the <see cref="ILogger"/> interface.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <remarks>
|
/// <remarks>
|
||||||
/// This implementation is also implementing the <see cref="IDisposable"/> interface!
|
/// This implementation is also using the <see cref="IDisposable"/> interface!
|
||||||
/// <br/>
|
/// <br/>
|
||||||
/// Inspired by <a href="https://github.com/aspnet/Logging/blob/2d2f31968229eddb57b6ba3d34696ef366a6c71b/src/Microsoft.Extensions.Logging.Console/ConsoleLogger.cs" />
|
/// Inspired by <see href="https://github.com/aspnet/Logging/blob/2d2f31968229eddb57b6ba3d34696ef366a6c71b/src/Microsoft.Extensions.Logging.Console/ConsoleLogger.cs">ConsoleLogger.cs</see>
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
/// <seealso cref="ILogger" />
|
/// <seealso cref="ILogger" />
|
||||||
/// <seealso cref="IDisposable" />
|
/// <seealso cref="IDisposable" />
|
||||||
|
|||||||
Reference in New Issue
Block a user