Commit 5df6c76c authored by Savorboard's avatar Savorboard

Fix SQL Server tranaction error in entityframework. #402

parent 45286ea2
...@@ -11,29 +11,22 @@ using DotNetCore.CAP.Persistence; ...@@ -11,29 +11,22 @@ using DotNetCore.CAP.Persistence;
using DotNetCore.CAP.SqlServer.Diagnostics; using DotNetCore.CAP.SqlServer.Diagnostics;
using DotNetCore.CAP.Transport; using DotNetCore.CAP.Transport;
using Microsoft.Data.SqlClient; using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage; using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
// 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( public SqlServerCapTransaction(
IDispatcher dispatcher, IDispatcher dispatcher,
IServiceProvider serviceProvider) : base(dispatcher) DiagnosticProcessorObserver diagnosticProcessor) : base(dispatcher)
{ {
var sqlServerOptions = serviceProvider.GetService<IOptions<SqlServerOptions>>().Value; _diagnosticProcessor = diagnosticProcessor;
if (sqlServerOptions.DbContextType != null)
_dbContext = serviceProvider.GetService(sqlServerOptions.DbContextType) as DbContext;
_diagnosticProcessor = serviceProvider.GetRequiredService<DiagnosticProcessorObserver>();
} }
protected override void AddToSent(MediumMessage msg) protected override void AddToSent(MediumMessage msg)
...@@ -76,7 +69,6 @@ namespace DotNetCore.CAP ...@@ -76,7 +69,6 @@ namespace DotNetCore.CAP
dbTransaction.Commit(); dbTransaction.Commit();
break; break;
case IDbContextTransaction dbContextTransaction: case IDbContextTransaction dbContextTransaction:
_dbContext?.SaveChanges();
dbContextTransaction.Commit(); dbContextTransaction.Commit();
break; break;
} }
...@@ -93,7 +85,6 @@ namespace DotNetCore.CAP ...@@ -93,7 +85,6 @@ namespace DotNetCore.CAP
dbTransaction.Commit(); dbTransaction.Commit();
break; break;
case IDbContextTransaction dbContextTransaction: case IDbContextTransaction dbContextTransaction:
await _dbContext.SaveChangesAsync(cancellationToken);
await dbContextTransaction.CommitAsync(cancellationToken); await dbContextTransaction.CommitAsync(cancellationToken);
break; 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