Commit 08ac461b authored by Savorboard's avatar Savorboard

add save change when transaction commit

parent ea6506d1
...@@ -14,7 +14,7 @@ using Microsoft.EntityFrameworkCore.Storage; ...@@ -14,7 +14,7 @@ using Microsoft.EntityFrameworkCore.Storage;
namespace DotNetCore.CAP.SqlServer namespace DotNetCore.CAP.SqlServer
{ {
public class SqlServerPublisher : CapPublisherBase, ICallbackPublisher public class SqlServerPublisher : CapPublisherBase, ICallbackPublisher, IDisposable
{ {
private readonly DbContext _dbContext; private readonly DbContext _dbContext;
private readonly SqlServerOptions _options; private readonly SqlServerOptions _options;
......
...@@ -7,20 +7,29 @@ using System.Data; ...@@ -7,20 +7,29 @@ using System.Data;
using System.Data.SqlClient; using System.Data.SqlClient;
using DotNetCore.CAP.Models; using DotNetCore.CAP.Models;
using DotNetCore.CAP.SqlServer.Diagnostics; using DotNetCore.CAP.SqlServer.Diagnostics;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage; using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.Extensions.DependencyInjection;
// ReSharper disable once CheckNamespace // ReSharper disable once CheckNamespace
namespace DotNetCore.CAP namespace DotNetCore.CAP
{ {
public class SqlServerCapTransaction : CapTransactionBase public class SqlServerCapTransaction : CapTransactionBase
{ {
private readonly DbContext _dbContext;
private readonly DiagnosticProcessorObserver _diagnosticProcessor; private readonly DiagnosticProcessorObserver _diagnosticProcessor;
public SqlServerCapTransaction(IDispatcher dispatcher, public SqlServerCapTransaction(
DiagnosticProcessorObserver diagnosticProcessor) : base(dispatcher) IDispatcher dispatcher,
SqlServerOptions sqlServerOptions,
IServiceProvider serviceProvider) : base(dispatcher)
{ {
_diagnosticProcessor = diagnosticProcessor; if (sqlServerOptions.DbContextType != null)
{
_dbContext = serviceProvider.GetService(sqlServerOptions.DbContextType) as DbContext;
}
_diagnosticProcessor = serviceProvider.GetRequiredService<DiagnosticProcessorObserver>();
} }
protected override void AddToSent(CapPublishedMessage msg) protected override void AddToSent(CapPublishedMessage msg)
...@@ -59,6 +68,7 @@ namespace DotNetCore.CAP ...@@ -59,6 +68,7 @@ namespace DotNetCore.CAP
dbTransaction.Commit(); dbTransaction.Commit();
break; break;
case IDbContextTransaction dbContextTransaction: case IDbContextTransaction dbContextTransaction:
_dbContext?.SaveChanges();
dbContextTransaction.Commit(); dbContextTransaction.Commit();
break; break;
} }
...@@ -79,7 +89,15 @@ namespace DotNetCore.CAP ...@@ -79,7 +89,15 @@ namespace DotNetCore.CAP
public override void Dispose() public override void Dispose()
{ {
switch (DbTransaction)
{
case IDbTransaction dbTransaction:
dbTransaction.Dispose();
break;
case IDbContextTransaction dbContextTransaction:
dbContextTransaction.Dispose();
break;
}
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment