From 467fb54625ed8ee56f1d3077f1d8bf48c18856f0 Mon Sep 17 00:00:00 2001 From: Savorboard Date: Tue, 21 Aug 2018 11:09:02 +0800 Subject: [PATCH] Add entityframework extension support --- .../ICapTransaction.MySql.cs | 9 +++++ .../IDbContextTransaction.CAP.cs | 35 +++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 src/DotNetCore.CAP.MySql/IDbContextTransaction.CAP.cs 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; } + } +}