Skip to content

Commit 7703c80

Browse files
committed
发布x.3.1.73修复未分表对象在非默认数据源中创建了表
1 parent dce2fe3 commit 7703c80

File tree

7 files changed

+93
-18
lines changed

7 files changed

+93
-18
lines changed

nuget-publish.bat

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
:start
22
::定义版本
3-
set EFCORE2=2.3.1.72
4-
set EFCORE3=3.3.1.72
5-
set EFCORE5=5.3.1.72
6-
set EFCORE6=6.3.1.72
3+
set EFCORE2=2.3.1.73
4+
set EFCORE3=3.3.1.73
5+
set EFCORE5=5.3.1.73
6+
set EFCORE6=6.3.1.73
77

88
::删除所有bin与obj下的文件
99
@echo off
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace Sample.SqlServerShardingAll.Entities
2+
{
3+
public class NoShardingTest
4+
{
5+
/// <summary>
6+
/// user id
7+
/// </summary>
8+
public string Id { get; set; }
9+
/// <summary>
10+
/// user name
11+
/// </summary>
12+
public string Name { get; set; }
13+
/// <summary>
14+
/// area
15+
/// </summary>
16+
public string Area { get; set; }
17+
}
18+
}

samples/Sample.SqlServerShardingAll/MyDbContext.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
3232
entity.Property(o=>o.Area).IsRequired().IsUnicode(false).HasMaxLength(50);
3333
entity.ToTable(nameof(SysUser));
3434
});
35+
modelBuilder.Entity<NoShardingTest>(entity =>
36+
{
37+
entity.HasKey(o => o.Id);
38+
entity.Property(o => o.Id).IsRequired().IsUnicode(false).HasMaxLength(50);
39+
entity.Property(o=>o.Name).IsRequired().IsUnicode(false).HasMaxLength(50);
40+
entity.Property(o=>o.Area).IsRequired().IsUnicode(false).HasMaxLength(50);
41+
entity.ToTable(nameof(NoShardingTest));
42+
});
3543
}
3644

3745
public IRouteTail RouteTail { get; set; }

src/ShardingCore/Bootstrapers/ShardingDbContextBootstrapper.cs

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,21 +79,12 @@ public ShardingDbContextBootstrapper(IShardingConfigOption shardingConfigOption)
7979
public void Init()
8080
{
8181
_virtualDataSource.AddPhysicDataSource(new DefaultPhysicDataSource(_shardingConfigOption.DefaultDataSourceName, _shardingConfigOption.DefaultConnectionString, true));
82-
InitializeParallelTables();
8382
InitializeEntityMetadata();
83+
InitializeParallelTables();
8484
InitializeConfigure();
8585
_virtualDataSource.CheckVirtualDataSource();
8686
}
8787

88-
private void InitializeParallelTables()
89-
{
90-
foreach (var parallelTableGroupNode in _shardingConfigOption.GetParallelTableGroupNodes())
91-
{
92-
_parallelTableManager.AddParallelTable(parallelTableGroupNode);
93-
}
94-
95-
}
96-
9788
private void InitializeEntityMetadata()
9889
{
9990
using (var serviceScope = ShardingContainer.ServiceProvider.CreateScope())
@@ -133,9 +124,22 @@ var constructors
133124

134125
}
135126

136-
private void InitializeConfigure()
127+
private void InitializeParallelTables()
137128
{
129+
foreach (var parallelTableGroupNode in _shardingConfigOption.GetParallelTableGroupNodes())
130+
{
131+
var parallelTableComparerType = parallelTableGroupNode.GetEntities().FirstOrDefault(o => !_entityMetadataManager.IsShardingTable(o.Type));
132+
if (parallelTableComparerType != null)
133+
{
134+
throw new ShardingCoreInvalidOperationException(
135+
$"{parallelTableComparerType.Type.Name} must is sharding table type");
136+
}
137+
_parallelTableManager.AddParallelTable(parallelTableGroupNode);
138+
}
139+
}
138140

141+
private void InitializeConfigure()
142+
{
139143
var dataSources = _shardingConfigOption.GetDataSources();
140144
foreach (var dataSourceKv in dataSources)
141145
{
@@ -230,7 +234,9 @@ private void EnsureCreated(DbContext context, string dataSourceName)
230234
if (context is IShardingDbContext shardingDbContext)
231235
{
232236
var dbContext = shardingDbContext.GetDbContext(dataSourceName, false, _routeTailFactory.Create(string.Empty));
233-
237+
238+
var isDefault = _virtualDataSource.IsDefault(dataSourceName);
239+
234240
var modelCacheSyncObject = dbContext.GetModelCacheSyncObject();
235241

236242
var acquire = Monitor.TryEnter(modelCacheSyncObject, TimeSpan.FromSeconds(3));
@@ -241,7 +247,14 @@ private void EnsureCreated(DbContext context, string dataSourceName)
241247

242248
try
243249
{
244-
dbContext.RemoveDbContextRelationModelThatIsShardingTable();
250+
if(isDefault)
251+
{
252+
dbContext.RemoveDbContextRelationModelThatIsShardingTable();
253+
}
254+
else
255+
{
256+
dbContext.RemoveDbContextAllRelationModel();
257+
}
245258
dbContext.Database.EnsureCreated();
246259
dbContext.RemoveModelCache();
247260
}

src/ShardingCore/Extensions/DbContextExtension.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,35 @@ public static void RemoveDbContextRelationModelThatIsShardingTable(this DbContex
6666
}
6767
#endif
6868
}
69+
/// <summary>
70+
/// 移除所有的分表关系的模型
71+
/// </summary>
72+
/// <param name="dbContext"></param>
73+
public static void RemoveDbContextAllRelationModel(this DbContext dbContext)
74+
{
75+
#if EFCORE6
76+
77+
var contextModel = dbContext.GetService<IDesignTimeModel>().Model; ;
78+
#endif
79+
#if EFCORE2 || EFCORE3 || EFCORE5
80+
81+
var contextModel = dbContext.Model as Model;
82+
#endif
83+
84+
#if EFCORE6
85+
var contextModelRelationalModel = contextModel.GetRelationalModel() as RelationalModel;
86+
contextModelRelationalModel.Tables.Clear();
87+
#endif
88+
#if EFCORE5
89+
var contextModelRelationalModel = contextModel.RelationalModel as RelationalModel;
90+
contextModelRelationalModel.Tables.Clear();
91+
#endif
92+
#if EFCORE2 || EFCORE3
93+
var entityTypes =
94+
contextModel.GetFieldValue("_entityTypes") as SortedDictionary<string, EntityType>;
95+
entityTypes.Clear();
96+
#endif
97+
}
6998

7099
/// <summary>
71100
/// 移除所有的除了我指定的那个类型

src/ShardingCore/Sharding/ParallelTables/ParallelTableGroupNode.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ public ParallelTableGroupNode(IEnumerable<ParallelTableComparerType> entities)
1818
{
1919
_entities = new SortedSet<ParallelTableComparerType>(entities);
2020
}
21+
22+
public ISet<ParallelTableComparerType> GetEntities()
23+
{
24+
return _entities;
25+
}
2126
protected bool Equals(ParallelTableGroupNode other)
2227
{
2328
return _entities.SequenceEqual(other._entities);

src/ShardingCore/Sharding/StreamMergeContext.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ public StreamMergeContext(IQueryable<TEntity> source, IShardingDbContext shardin
8989
_routeTailFactory = routeTailFactory;
9090
DataSourceRouteResult = dataSourceRouteResult;
9191
_parallelTableManager = (IParallelTableManager)ShardingContainer.GetService(typeof(IParallelTableManager<>).GetGenericType0(shardingDbContext.GetType()));
92-
92+
_entityMetadataManager =
93+
(IEntityMetadataManager)ShardingContainer.GetService(
94+
typeof(IEntityMetadataManager<>).GetGenericType0(shardingDbContext.GetType()));
9395
TableRouteResults = GetTableRouteResults(tableRouteResults);
9496
IsCrossDataSource = dataSourceRouteResult.IntersectDataSources.Count > 1;
9597
IsCrossTable = TableRouteResults.Count() > 1;

0 commit comments

Comments
 (0)