mysql table ensure manager添加null判断处理

This commit is contained in:
xuejiaming 2022-03-15 10:53:01 +08:00
parent b3b43245f9
commit 517ee61086
7 changed files with 61 additions and 5 deletions

View File

@ -41,6 +41,7 @@ namespace Sample.SqlServer.Controllers
[HttpGet]
public async Task<IActionResult> Get2x()
{
_defaultTableDbContext.ChangeTracker.HasChanges()
//var queryable = _defaultTableDbContext.Set<SysUserMod>().Where(o=>true);
//var tableRouteRuleEngineFactory = ShardingContainer.GetService<ITableRouteRuleEngineFactory<DefaultShardingDbContext>>();

View File

@ -1,4 +1,6 @@
using System;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
using ShardingCore.Core;
namespace Sample.SqlServer.Domain.Entities
@ -12,6 +14,7 @@ namespace Sample.SqlServer.Domain.Entities
/// <summary>
/// 用户表
/// </summary>
//[Index("Name")]
public class SysUserMod
{
/// <summary>

View File

@ -14,8 +14,8 @@ namespace Sample.SqlServer.Domain.Maps
{
public void Configure(EntityTypeBuilder<SysUserMod> builder)
{
//builder.HasKey(o => o.Id);
builder.HasNoKey();
builder.HasKey(o => o.Id);
//builder.HasNoKey();
builder.Property(o => o.Id).IsRequired().HasMaxLength(128);
builder.Property(o => o.Name).HasMaxLength(128);
builder.ToTable(nameof(SysUserMod));

View File

@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
using Microsoft.EntityFrameworkCore.Metadata;
using ShardingCore.Exceptions;
using ShardingCore.Sharding.Abstractions;
namespace ShardingCore.EFCores
{
public class ShardingChangeTracker: ChangeTracker
{
private readonly ICurrentDbContextDiscover _contextDiscover;
public ShardingChangeTracker(DbContext context, IStateManager stateManager, IChangeDetector changeDetector, IModel model, IEntityEntryGraphIterator graphIterator) : base(context, stateManager, changeDetector, model, graphIterator)
{
_contextDiscover = context as ICurrentDbContextDiscover?? throw new ShardingCoreNotSupportException($"{context.GetType()} not impl {nameof(ICurrentDbContextDiscover)}");
}
public override bool HasChanges()
{
return _contextDiscover.GetCurrentDbContexts().Any(o =>
o.Value.GetCurrentContexts().Any(r => r.Value.ChangeTracker.HasChanges()));
}
}
}

View File

@ -27,7 +27,7 @@ namespace ShardingCore.Sharding
/// <summary>
/// 分表分库的dbcontext
/// </summary>
public abstract class AbstractShardingDbContext : DbContext, IShardingDbContext, ISupportShardingTransaction, ISupportShardingReadWrite
public abstract class AbstractShardingDbContext : DbContext, IShardingDbContext, ISupportShardingTransaction, ISupportShardingReadWrite//,ICurrentDbContextDiscover
{
protected IShardingDbContextExecutor ShardingDbContextExecutor { get; }
@ -505,5 +505,9 @@ namespace ShardingCore.Sharding
ShardingDbContextExecutor.Commit();
}
//public IDictionary<string, IDataSourceDbContext> GetCurrentDbContexts()
//{
// return ShardingDbContextExecutor.GetCurrentDbContexts();
//}
}
}

View File

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ShardingCore.Sharding.ShardingDbContextExecutors;
namespace ShardingCore.Sharding.Abstractions
{
public interface ICurrentDbContextDiscover
{
IDictionary<string, IDataSourceDbContext> GetCurrentDbContexts();
}
}

View File

@ -24,8 +24,12 @@ namespace ShardingCore.TableExists
{
for (int i = 0; i < dataTable.Rows.Count; i++)
{
if (database.Equals(dataTable.Rows[i][TABLE_SCHEMA]?.ToString()??String.Empty,StringComparison.OrdinalIgnoreCase))
result.Add(dataTable.Rows[i][TABLE_NAME].ToString());
var schema = dataTable.Rows[i][TABLE_SCHEMA];
if (database.Equals($"{schema}", StringComparison.OrdinalIgnoreCase))
{
var tableName = dataTable.Rows[i][TABLE_NAME];
result.Add($"{tableName}");
}
}
}
return result;