diff --git a/src/DotNetCore.CAP.MySql/ICapTransaction.MySql.cs b/src/DotNetCore.CAP.MySql/ICapTransaction.MySql.cs index 5a0bef8..7a79f91 100644 --- a/src/DotNetCore.CAP.MySql/ICapTransaction.MySql.cs +++ b/src/DotNetCore.CAP.MySql/ICapTransaction.MySql.cs @@ -1,6 +1,7 @@ using System.Data; using System.Diagnostics; using Microsoft.EntityFrameworkCore.Storage; +using Microsoft.EntityFrameworkCore.Infrastructure; // ReSharper disable once CheckNamespace namespace DotNetCore.CAP @@ -60,5 +61,13 @@ namespace DotNetCore.CAP return transaction; } + + public static IDbContextTransaction BeginAndJoinToTransaction(this DatabaseFacade database, + ICapPublisher publisher, bool autoCommit = false) + { + var trans = database.BeginTransaction(); + var capTrans = publisher.Transaction.Begin(trans); + return new CapEFDbTransaction(capTrans); + } } } \ No newline at end of file diff --git a/src/DotNetCore.CAP.MySql/IDbContextTransaction.CAP.cs b/src/DotNetCore.CAP.MySql/IDbContextTransaction.CAP.cs new file mode 100644 index 0000000..dbfa691 --- /dev/null +++ b/src/DotNetCore.CAP.MySql/IDbContextTransaction.CAP.cs @@ -0,0 +1,35 @@ +using System; +using DotNetCore.CAP; + +// ReSharper disable once CheckNamespace +namespace Microsoft.EntityFrameworkCore.Storage +{ + internal class CapEFDbTransaction : IDbContextTransaction + { + private readonly ICapTransaction _transaction; + + public CapEFDbTransaction(ICapTransaction transaction) + { + _transaction = transaction; + var dbContextTransaction = (IDbContextTransaction)_transaction.DbTransaction; + TransactionId = dbContextTransaction.TransactionId; + } + + public void Dispose() + { + _transaction.Dispose(); + } + + public void Commit() + { + _transaction.Commit(); + } + + public void Rollback() + { + _transaction.Rollback(); + } + + public Guid TransactionId { get; } + } +}