修复efcore2编译错误
This commit is contained in:
parent
e9226e2773
commit
04a069677b
|
@ -1,179 +1,179 @@
|
||||||
using System;
|
//using System;
|
||||||
using System.Collections.Concurrent;
|
//using System.Collections.Concurrent;
|
||||||
using System.Collections.Generic;
|
//using System.Collections.Generic;
|
||||||
using System.Data;
|
//using System.Data;
|
||||||
using System.Data.Common;
|
//using System.Data.Common;
|
||||||
using System.Linq;
|
//using System.Linq;
|
||||||
using System.Text;
|
//using System.Text;
|
||||||
using System.Threading;
|
//using System.Threading;
|
||||||
using System.Threading.Tasks;
|
//using System.Threading.Tasks;
|
||||||
using Microsoft.EntityFrameworkCore;
|
//using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.EntityFrameworkCore.Storage;
|
//using Microsoft.EntityFrameworkCore.Storage;
|
||||||
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
|
//using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
|
||||||
using ShardingCore.DbContexts;
|
//using ShardingCore.DbContexts;
|
||||||
using ShardingCore.DbContexts.ShardingDbContexts;
|
//using ShardingCore.DbContexts.ShardingDbContexts;
|
||||||
using ShardingCore.Sharding.Abstractions;
|
//using ShardingCore.Sharding.Abstractions;
|
||||||
using ShardingCore.Sharding.ShardingTransactions;
|
//using ShardingCore.Sharding.ShardingTransactions;
|
||||||
|
|
||||||
namespace ShardingCore.Sharding.ShardingDbContextExecutors
|
//namespace ShardingCore.Sharding.ShardingDbContextExecutors
|
||||||
{
|
//{
|
||||||
/*
|
// /*
|
||||||
* @Author: xjm
|
// * @Author: xjm
|
||||||
* @Description:
|
// * @Description:
|
||||||
* @Date: 2021/9/30 10:53:23
|
// * @Date: 2021/9/30 10:53:23
|
||||||
* @Ver: 1.0
|
// * @Ver: 1.0
|
||||||
* @Email: 326308290@qq.com
|
// * @Email: 326308290@qq.com
|
||||||
*/
|
// */
|
||||||
public class DataSourceDbContext<TShardingDbContext> : IDisposable
|
// public class DataSourceDbContext<TShardingDbContext> : IDisposable
|
||||||
#if !EFCORE2
|
//#if !EFCORE2
|
||||||
, IAsyncDisposable
|
// , IAsyncDisposable
|
||||||
#endif
|
//#endif
|
||||||
where TShardingDbContext : DbContext, IShardingDbContext
|
// where TShardingDbContext : DbContext, IShardingDbContext
|
||||||
{
|
// {
|
||||||
/// <summary>
|
// /// <summary>
|
||||||
/// 数据源名称
|
// /// 数据源名称
|
||||||
/// </summary>
|
// /// </summary>
|
||||||
public string DataSourceName { get; }
|
// public string DataSourceName { get; }
|
||||||
private readonly IShardingDbContextFactory<TShardingDbContext> _shardingDbContextFactory;
|
// private readonly IShardingDbContextFactory<TShardingDbContext> _shardingDbContextFactory;
|
||||||
|
|
||||||
private ConcurrentDictionary<string, DbContext> _dataSourceDbContexts =
|
// private ConcurrentDictionary<string, DbContext> _dataSourceDbContexts =
|
||||||
new ConcurrentDictionary<string, DbContext>();
|
// new ConcurrentDictionary<string, DbContext>();
|
||||||
|
|
||||||
private IDbContextTransaction _dbContextTransaction;
|
// private IDbContextTransaction _dbContextTransaction;
|
||||||
private IsolationLevel isolationLevel = IsolationLevel.Unspecified;
|
// private IsolationLevel isolationLevel = IsolationLevel.Unspecified;
|
||||||
|
|
||||||
private bool _isBeginTransaction;
|
// private bool _isBeginTransaction;
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
// /// <summary>
|
||||||
///
|
// ///
|
||||||
/// </summary>
|
// /// </summary>
|
||||||
/// <param name="dataSourceName"></param>
|
// /// <param name="dataSourceName"></param>
|
||||||
/// <param name="shardingDbContextFactory"></param>
|
// /// <param name="shardingDbContextFactory"></param>
|
||||||
/// <param name="isBeginTransaction"></param>
|
// /// <param name="isBeginTransaction"></param>
|
||||||
public DataSourceDbContext(string dataSourceName, IShardingDbContextFactory<TShardingDbContext> shardingDbContextFactory, bool isBeginTransaction)
|
// public DataSourceDbContext(string dataSourceName, IShardingDbContextFactory<TShardingDbContext> shardingDbContextFactory, bool isBeginTransaction)
|
||||||
{
|
// {
|
||||||
DataSourceName = dataSourceName;
|
// DataSourceName = dataSourceName;
|
||||||
_shardingDbContextFactory = shardingDbContextFactory;
|
// _shardingDbContextFactory = shardingDbContextFactory;
|
||||||
_isBeginTransaction = isBeginTransaction;
|
// _isBeginTransaction = isBeginTransaction;
|
||||||
}
|
// }
|
||||||
public void BeginTransaction(IsolationLevel isolationLevel = IsolationLevel.Unspecified)
|
// public void BeginTransaction(IsolationLevel isolationLevel = IsolationLevel.Unspecified)
|
||||||
{
|
// {
|
||||||
if (_isBeginTransaction)
|
// if (_isBeginTransaction)
|
||||||
throw new InvalidOperationException("transaction is already begin");
|
// throw new InvalidOperationException("transaction is already begin");
|
||||||
_isBeginTransaction = true;
|
// _isBeginTransaction = true;
|
||||||
this.isolationLevel = isolationLevel;
|
// this.isolationLevel = isolationLevel;
|
||||||
}
|
// }
|
||||||
public bool IsEmpty()
|
// public bool IsEmpty()
|
||||||
{
|
// {
|
||||||
return !_dataSourceDbContexts.Any();
|
// return !_dataSourceDbContexts.Any();
|
||||||
}
|
// }
|
||||||
|
|
||||||
public DbContext TryGetOrCreateDbContext(IRouteTail routeTail, ShardingDbContextOptions shardingDbContextOptions)
|
// public DbContext TryGetOrCreateDbContext(IRouteTail routeTail, ShardingDbContextOptions shardingDbContextOptions)
|
||||||
{
|
// {
|
||||||
var cacheKey = routeTail.GetRouteTailIdentity();
|
// var cacheKey = routeTail.GetRouteTailIdentity();
|
||||||
|
|
||||||
if (!_dataSourceDbContexts.TryGetValue(cacheKey, out var dbContext))
|
// if (!_dataSourceDbContexts.TryGetValue(cacheKey, out var dbContext))
|
||||||
{
|
// {
|
||||||
dbContext = _shardingDbContextFactory.Create(shardingDbContextOptions);
|
// dbContext = _shardingDbContextFactory.Create(shardingDbContextOptions);
|
||||||
if (_isBeginTransaction)
|
// if (_isBeginTransaction)
|
||||||
{
|
// {
|
||||||
if (_dbContextTransaction == null)
|
// if (_dbContextTransaction == null)
|
||||||
{
|
// {
|
||||||
_dbContextTransaction = dbContext.Database.BeginTransaction(isolationLevel);
|
// _dbContextTransaction = dbContext.Database.BeginTransaction(isolationLevel);
|
||||||
}
|
// }
|
||||||
UseTransaction(_dbContextTransaction);
|
// UseTransaction(_dbContextTransaction);
|
||||||
}
|
// }
|
||||||
_dataSourceDbContexts.TryAdd(cacheKey, dbContext);
|
// _dataSourceDbContexts.TryAdd(cacheKey, dbContext);
|
||||||
}
|
// }
|
||||||
return dbContext;
|
// return dbContext;
|
||||||
}
|
// }
|
||||||
|
|
||||||
public DbConnection GetDbConnection()
|
// public DbConnection GetDbConnection()
|
||||||
{
|
// {
|
||||||
return _dataSourceDbContexts.First().Value.Database.GetDbConnection();
|
// return _dataSourceDbContexts.First().Value.Database.GetDbConnection();
|
||||||
}
|
// }
|
||||||
|
|
||||||
public void UseTransaction(IDbContextTransaction dbContextTransaction)
|
// public void UseTransaction(IDbContextTransaction dbContextTransaction)
|
||||||
{
|
// {
|
||||||
if (dbContextTransaction == null)
|
// if (dbContextTransaction == null)
|
||||||
{
|
// {
|
||||||
foreach (var dataSourceDbContext in _dataSourceDbContexts)
|
// foreach (var dataSourceDbContext in _dataSourceDbContexts)
|
||||||
{
|
// {
|
||||||
if (dataSourceDbContext.Value.Database.CurrentTransaction != null)
|
// if (dataSourceDbContext.Value.Database.CurrentTransaction != null)
|
||||||
dataSourceDbContext.Value.Database.UseTransaction(null);
|
// dataSourceDbContext.Value.Database.UseTransaction(null);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
else
|
// else
|
||||||
{
|
// {
|
||||||
foreach (var dataSourceDbContext in _dataSourceDbContexts)
|
// foreach (var dataSourceDbContext in _dataSourceDbContexts)
|
||||||
{
|
// {
|
||||||
if (dataSourceDbContext.Value.Database.CurrentTransaction == null)
|
// if (dataSourceDbContext.Value.Database.CurrentTransaction == null)
|
||||||
dataSourceDbContext.Value.Database.UseTransaction(dbContextTransaction.GetDbTransaction());
|
// dataSourceDbContext.Value.Database.UseTransaction(dbContextTransaction.GetDbTransaction());
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
public async Task UseTransactionAsync(IDbContextTransaction dbContextTransaction, CancellationToken cancellationToken = new CancellationToken())
|
// public async Task UseTransactionAsync(IDbContextTransaction dbContextTransaction, CancellationToken cancellationToken = new CancellationToken())
|
||||||
{
|
// {
|
||||||
cancellationToken.ThrowIfCancellationRequested();
|
// cancellationToken.ThrowIfCancellationRequested();
|
||||||
if (dbContextTransaction == null)
|
// if (dbContextTransaction == null)
|
||||||
{
|
// {
|
||||||
foreach (var dataSourceDbContext in _dataSourceDbContexts)
|
// foreach (var dataSourceDbContext in _dataSourceDbContexts)
|
||||||
{
|
// {
|
||||||
if (dataSourceDbContext.Value.Database.CurrentTransaction != null)
|
// if (dataSourceDbContext.Value.Database.CurrentTransaction != null)
|
||||||
await dataSourceDbContext.Value.Database.UseTransactionAsync(null, cancellationToken);
|
// await dataSourceDbContext.Value.Database.UseTransactionAsync(null, cancellationToken);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
else
|
// else
|
||||||
{
|
// {
|
||||||
foreach (var dataSourceDbContext in _dataSourceDbContexts)
|
// foreach (var dataSourceDbContext in _dataSourceDbContexts)
|
||||||
{
|
// {
|
||||||
if (dataSourceDbContext.Value.Database.CurrentTransaction == null)
|
// if (dataSourceDbContext.Value.Database.CurrentTransaction == null)
|
||||||
await dataSourceDbContext.Value.Database.UseTransactionAsync(dbContextTransaction.GetDbTransaction(), cancellationToken);
|
// await dataSourceDbContext.Value.Database.UseTransactionAsync(dbContextTransaction.GetDbTransaction(), cancellationToken);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
/// <summary>
|
// /// <summary>
|
||||||
/// 提交
|
// /// 提交
|
||||||
/// </summary>
|
// /// </summary>
|
||||||
/// <param name="acceptAllChangesOnSuccess"></param>
|
// /// <param name="acceptAllChangesOnSuccess"></param>
|
||||||
/// <returns></returns>
|
// /// <returns></returns>
|
||||||
public int SaveChanges(bool acceptAllChangesOnSuccess)
|
// public int SaveChanges(bool acceptAllChangesOnSuccess)
|
||||||
{
|
// {
|
||||||
int i = 0;
|
// int i = 0;
|
||||||
foreach (var dataSourceDbContext in _dataSourceDbContexts)
|
// foreach (var dataSourceDbContext in _dataSourceDbContexts)
|
||||||
{
|
// {
|
||||||
i += dataSourceDbContext.Value.SaveChanges(acceptAllChangesOnSuccess);
|
// i += dataSourceDbContext.Value.SaveChanges(acceptAllChangesOnSuccess);
|
||||||
}
|
// }
|
||||||
|
|
||||||
return i;
|
// return i;
|
||||||
}
|
// }
|
||||||
public async Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = new CancellationToken())
|
// public async Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = new CancellationToken())
|
||||||
{
|
// {
|
||||||
|
|
||||||
int i = 0;
|
// int i = 0;
|
||||||
foreach (var dataSourceDbContext in _dataSourceDbContexts)
|
// foreach (var dataSourceDbContext in _dataSourceDbContexts)
|
||||||
{
|
// {
|
||||||
i += await dataSourceDbContext.Value.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
|
// i += await dataSourceDbContext.Value.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
|
||||||
}
|
// }
|
||||||
|
|
||||||
return i;
|
// return i;
|
||||||
}
|
// }
|
||||||
|
|
||||||
public void Dispose()
|
// public void Dispose()
|
||||||
{
|
// {
|
||||||
foreach (var dataSourceDbContext in _dataSourceDbContexts)
|
// foreach (var dataSourceDbContext in _dataSourceDbContexts)
|
||||||
{
|
// {
|
||||||
dataSourceDbContext.Value.Dispose();
|
// dataSourceDbContext.Value.Dispose();
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
public async ValueTask DisposeAsync()
|
// public async ValueTask DisposeAsync()
|
||||||
{
|
// {
|
||||||
foreach (var dataSourceDbContext in _dataSourceDbContexts)
|
// foreach (var dataSourceDbContext in _dataSourceDbContexts)
|
||||||
{
|
// {
|
||||||
await dataSourceDbContext.Value.DisposeAsync();
|
// await dataSourceDbContext.Value.DisposeAsync();
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
//}
|
||||||
|
|
Loading…
Reference in New Issue