This commit is contained in:
xuejiaming 2021-09-22 20:35:19 +08:00
parent ce30a09fb6
commit 0b7ef8d6c2
6 changed files with 33 additions and 16 deletions

View File

@ -12,7 +12,6 @@ using Sample.SqlServer.Domain.Entities;
using ShardingCore.Core.QueryRouteManagers.Abstractions;
using ShardingCore.DbContexts.VirtualDbContexts;
using ShardingCore.Extensions;
using Z.EntityFramework.Plus;
namespace Sample.SqlServer.Controllers
{
@ -71,11 +70,11 @@ namespace Sample.SqlServer.Controllers
var sresultxasdc = _defaultTableDbContext.Set<SysUserMod>().Where(o => o.Id == "198").ToList();
var sresult = _defaultTableDbContext.Set<SysUserMod>().ToList();
var sysUserMod98 = result.FirstOrDefault(o => o.Id == "98");
_defaultTableDbContext.Attach(sysUserMod98);
sysUserMod98.Name = "name_update" + new Random().Next(1, 99) + "_98";
_defaultTableDbContext.Attach(sysUserMod98);
await _defaultTableDbContext.SaveChangesAsync();
//var sysUserMod98 = result.FirstOrDefault(o => o.Id == "98");
//_defaultTableDbContext.Attach(sysUserMod98);
//sysUserMod98.Name = "name_update" + new Random().Next(1, 99) + "_98";
//_defaultTableDbContext.Attach(sysUserMod98);
//await _defaultTableDbContext.SaveChangesAsync();
var stu = new STU() { Id = "198" };
var sresultx111x = _defaultTableDbContext.Set<SysUserMod>().FirstOrDefault(o => o.Id == stu.Id);
@ -88,6 +87,8 @@ namespace Sample.SqlServer.Controllers
var mod00s = await _defaultTableDbContext.Set<SysUserMod>().Skip(10).Take(11).ToListAsync();
}
_defaultTableDbContext.RemoveRange(_defaultTableDbContext.Set<SysUserMod>());
await _defaultTableDbContext.SaveChangesAsync();
return Ok();
}
[HttpGet]

View File

@ -6,10 +6,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="EFCore.BulkExtensions" Version="5.3.8" />
<PackageReference Include="EfCore.SqlServer2008Query" Version="1.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.9" />
<PackageReference Include="Z.EntityFramework.Plus.EFCore" Version="5.2.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.10" />
</ItemGroup>
<ItemGroup>

View File

@ -30,15 +30,15 @@ namespace Sample.SqlServer
//services.AddDbContext<DefaultTableDbContext>(o => o.UseSqlServer("Data Source=localhost;Initial Catalog=ShardingCoreDBxx3;Integrated Security=True"));
services.AddShardingDbContext<DefaultShardingDbContext, DefaultTableDbContext>(
o => o.UseSqlServer(
"Data Source=localhost;Initial Catalog=ShardingCoreDB;Integrated Security=True;")
).Begin(true)
o =>
o.UseSqlServer("Data Source=localhost;Initial Catalog=ShardingCoreDB1;Integrated Security=True;")
).Begin(true,true)
.AddShardingQuery((conStr, builder) => builder.UseSqlServer(conStr).UseLoggerFactory(efLogger)
.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking))
.AddShardingTransaction((connection, builder) =>
builder.UseSqlServer(connection).UseLoggerFactory(efLogger))
.AddDefaultDataSource("ds0",
"Data Source=localhost;Initial Catalog=ShardingCoreDB;Integrated Security=True;")
"Data Source=localhost;Initial Catalog=ShardingCoreDB1;Integrated Security=True;")
.AddShardingTable(o =>
{
o.AddShardingTableRoute<SysUserModVirtualTableRoute>();

View File

@ -109,6 +109,19 @@ var contextModelRelationalModel = contextModel.RelationalModel as RelationalMode
object key1 = modelSourceDependencies.ModelCacheKeyFactory.Create(dbContext);
memoryCache.Remove(key1);
#endif
#if EFCORE2
var modelSource = serviceScope.ServiceProvider.GetService<IModelSource>();
var modelSourceImpl = modelSource as RelationalModelSource;
var modelSourceDependencies =
modelSourceImpl.GetPropertyValue("Dependencies") as ModelSourceDependencies;
var models =
typeof(ModelSource).GetTypeFieldValue(modelSourceImpl, "_models") as ConcurrentDictionary<object, Lazy<IModel>>;
object key1 = modelSourceDependencies.ModelCacheKeyFactory.Create(dbContext);
models.TryRemove(key1, out var del);
#endif
}
/// <summary>

View File

@ -399,6 +399,7 @@ namespace ShardingCore.Sharding
using (var tran = _shardingDbContextExecutor.BeginTransaction())
{
i = _shardingDbContextExecutor.SaveChanges(acceptAllChangesOnSuccess);
tran.Commit();
}
}
else

View File

@ -109,7 +109,8 @@ namespace ShardingCore.Sharding.Enumerators
#if EFCORE2
public void Dispose()
{
_asyncSource.Dispose();
_asyncSource?.Dispose();
_syncSource?.Dispose();
}
public async Task<bool> MoveNext(CancellationToken cancellationToken = new CancellationToken())
@ -134,7 +135,11 @@ namespace ShardingCore.Sharding.Enumerators
{
if (tryGetCurrentError)
return default;
return _asyncSource.Current;
if (_asyncSource!= null)
return _asyncSource.Current;
if (_syncSource != null)
return _syncSource.Current;
return default;
}
catch (Exception e)
{