Skip to content

Commit 9485ae0

Browse files
committed
修复[#214] 发布6.8.0.3
1 parent 9048048 commit 9485ae0

File tree

14 files changed

+160
-39
lines changed

14 files changed

+160
-39
lines changed

nuget-publish.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
:start
22
::定义版本
3-
set SHARDINGCORE=6.8.0.2
3+
set SHARDINGCORE=6.8.0.3
44

55
::删除所有bin与obj下的文件
66
@echo off

samples/Sample.MySql/Controllers/WeatherForecastController.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Sample.MySql.multi;
66
using Sample.MySql.Shardings;
77
using ShardingCore.Core.RuntimeContexts;
8+
using ShardingCore.Core.VirtualRoutes.TableRoutes;
89
using ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions;
910
using ShardingCore.Extensions.ShardingQueryableExtensions;
1011
using ShardingCore.Helpers;
@@ -85,6 +86,11 @@ public async Task<IActionResult> Get()
8586
// Console.WriteLine("------------");
8687
// using (var tran = _defaultTableDbContext.Database.BeginTransaction())
8788
// {
89+
90+
SysUserMod? resultX123 = await _defaultTableDbContext.Set<SysUserMod>()
91+
.Where(o => o.Id == "2").FirstOrDefaultAsync();
92+
_defaultTableDbContext.Update(resultX123);
93+
_defaultTableDbContext.SaveChanges();
8894
var resultX1 = await _defaultTableDbContext.Set<SysUserMod>()
8995
.Where(o => o.Id == "2" || o.Id == "3").GroupBy(o => new { o.Id,o.Name })
9096
.Select(o => new

samples/Sample.MySql/DbContexts/DefaultShardingDbContext.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ public DefaultShardingDbContext(DbContextOptions<DefaultShardingDbContext> optio
1919
//ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
2020
//Database.SetCommandTimeout(30000);
2121
}
22+
23+
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
24+
{
25+
base.OnConfiguring(optionsBuilder);
26+
optionsBuilder.UseLazyLoadingProxies();
27+
}
28+
2229
private readonly MethodInfo? _configureGlobalFiltersMethodInfo =
2330
typeof(DefaultShardingDbContext).GetMethod(nameof(ConfigureGlobalFilters), BindingFlags.Instance | BindingFlags.NonPublic);
2431

samples/Sample.MySql/Sample.MySql.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10+
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="6.0.10" />
1011
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.8">
1112
<PrivateAssets>all</PrivateAssets>
1213
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

samples/Sample.MySql/Startup.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ public void ConfigureServices(IServiceCollection services)
103103
o.AddShardingDataSourceRoute<SysUserModVirtualDataSourceRoute>();
104104
}).UseConfig(o =>
105105
{
106+
o.UseEntityFrameworkCoreProxies = true;
106107
o.ThrowIfQueryRouteNotMatch = false;
107108
o.AutoUseWriteConnectionStringAfterWriteDb = true;
108109
o.UseShardingQuery((conStr, builder) =>

src/ShardingCore/Core/EntityMetadatas/EntityMetadata.cs

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@ namespace ShardingCore.Core.EntityMetadatas
1616
/// 分表或者分库对象的元数据信息记录对象在ShardingCore框架下的一些简单的信息
1717
/// </summary>
1818
public class EntityMetadata
19-
{private const string QueryFilter = "QueryFilter";
19+
{
20+
private const string QueryFilter = "QueryFilter";
21+
2022
public EntityMetadata(Type entityType)
2123
{
2224
EntityType = entityType;
2325
ShardingDataSourceProperties = new Dictionary<string, PropertyInfo>();
2426
ShardingTableProperties = new Dictionary<string, PropertyInfo>();
2527
}
28+
2629
/// <summary>
2730
/// 分表类型 sharding entity type
2831
/// </summary>
@@ -37,10 +40,12 @@ public EntityMetadata(Type entityType)
3740
/// 是否分表
3841
/// </summary>
3942
public bool IsMultiTableMapping => null != ShardingTableProperty;
43+
4044
/// <summary>
4145
/// 分库字段
4246
/// </summary>
4347
public PropertyInfo ShardingDataSourceProperty { get; private set; }
48+
4449
/// <summary>
4550
/// 分库所有字段包括 ShardingDataSourceProperty
4651
/// </summary>
@@ -49,37 +54,39 @@ public EntityMetadata(Type entityType)
4954
/// <summary>
5055
/// 启动时是否建表 auto create data source when start app
5156
/// </summary>
52-
public bool? AutoCreateDataSourceTable { get; set; }
57+
public bool? AutoCreateDataSourceTable { get; set; }
5358

5459
/// <summary>
5560
/// 分表字段 sharding table property
5661
/// </summary>
5762
public PropertyInfo ShardingTableProperty { get; private set; }
63+
5864
/// <summary>
5965
/// 分表所有字段包括 ShardingTableProperty
6066
/// </summary>
61-
public IDictionary<string, PropertyInfo> ShardingTableProperties { get;}
67+
public IDictionary<string, PropertyInfo> ShardingTableProperties { get; }
6268

6369

6470
/// <summary>
6571
/// 启动时是否建表 auto create table when start app
6672
/// </summary>
67-
public bool? AutoCreateTable { get; set; }
73+
public bool? AutoCreateTable { get; set; }
6874

6975
/// <summary>
7076
/// 分表隔离器 table sharding tail prefix
7177
/// </summary>
7278
public string TableSeparator { get; private set; } = "_";
73-
79+
7480
/// <summary>
7581
/// 逻辑表名
7682
/// </summary>
7783
public string LogicTableName { get; private set; }
78-
84+
7985
/// <summary>
8086
/// 主键
8187
/// </summary>
8288
public IReadOnlyList<PropertyInfo> PrimaryKeyProperties { get; private set; }
89+
8390
/**
8491
* efcore query filter
8592
*/
@@ -93,14 +100,14 @@ public EntityMetadata(Type entityType)
93100
public void SetEntityModel(IEntityType dbEntityType)
94101
{
95102
LogicTableName = dbEntityType.GetEntityTypeTableName();
96-
QueryFilterExpression= dbEntityType.GetAnnotations().FirstOrDefault(o=>o.Name== QueryFilter)?.Value as LambdaExpression;
103+
QueryFilterExpression =
104+
dbEntityType.GetAnnotations().FirstOrDefault(o => o.Name == QueryFilter)?.Value as LambdaExpression;
97105
PrimaryKeyProperties = dbEntityType.FindPrimaryKey()?.Properties?.Select(o => o.PropertyInfo)?.ToList() ??
98106
new List<PropertyInfo>();
99-
IsSingleKey=PrimaryKeyProperties.Count == 1;
107+
IsSingleKey = PrimaryKeyProperties.Count == 1;
100108
}
101-
102-
103-
109+
110+
104111
/// <summary>
105112
/// 设置分库字段
106113
/// </summary>
@@ -109,10 +116,12 @@ public void SetShardingDataSourceProperty(PropertyInfo propertyInfo)
109116
{
110117
Check.NotNull(propertyInfo, nameof(propertyInfo));
111118
if (ShardingDataSourceProperties.ContainsKey(propertyInfo.Name))
112-
throw new ShardingCoreConfigException($"same sharding data source property name:[{propertyInfo.Name}] don't repeat add");
119+
throw new ShardingCoreConfigException(
120+
$"same sharding data source property name:[{propertyInfo.Name}] don't repeat add");
113121
ShardingDataSourceProperty = propertyInfo;
114122
ShardingDataSourceProperties.Add(propertyInfo.Name, propertyInfo);
115123
}
124+
116125
/// <summary>
117126
/// 添加额外分表字段
118127
/// </summary>
@@ -121,9 +130,11 @@ public void SetShardingDataSourceProperty(PropertyInfo propertyInfo)
121130
public void AddExtraSharingDataSourceProperty(PropertyInfo propertyInfo)
122131
{
123132
if (ShardingDataSourceProperties.ContainsKey(propertyInfo.Name))
124-
throw new ShardingCoreConfigException($"same sharding data source property name:[{propertyInfo.Name}] don't repeat add");
133+
throw new ShardingCoreConfigException(
134+
$"same sharding data source property name:[{propertyInfo.Name}] don't repeat add");
125135
ShardingDataSourceProperties.Add(propertyInfo.Name, propertyInfo);
126136
}
137+
127138
/// <summary>
128139
/// 设置分表字段
129140
/// </summary>
@@ -132,10 +143,12 @@ public void SetShardingTableProperty(PropertyInfo propertyInfo)
132143
{
133144
Check.NotNull(propertyInfo, nameof(propertyInfo));
134145
if (ShardingTableProperties.ContainsKey(propertyInfo.Name))
135-
throw new ShardingCoreConfigException($"same sharding table property name:[{propertyInfo.Name}] don't repeat add");
146+
throw new ShardingCoreConfigException(
147+
$"same sharding table property name:[{propertyInfo.Name}] don't repeat add");
136148
ShardingTableProperty = propertyInfo;
137149
ShardingTableProperties.Add(propertyInfo.Name, propertyInfo);
138150
}
151+
139152
/// <summary>
140153
/// 添加额外分表字段
141154
/// </summary>
@@ -144,7 +157,8 @@ public void SetShardingTableProperty(PropertyInfo propertyInfo)
144157
public void AddExtraSharingTableProperty(PropertyInfo propertyInfo)
145158
{
146159
if (ShardingTableProperties.ContainsKey(propertyInfo.Name))
147-
throw new ShardingCoreConfigException($"same sharding table property name:[{propertyInfo.Name}] don't repeat add");
160+
throw new ShardingCoreConfigException(
161+
$"same sharding table property name:[{propertyInfo.Name}] don't repeat add");
148162
ShardingTableProperties.Add(propertyInfo.Name, propertyInfo);
149163
}
150164

@@ -156,6 +170,7 @@ public void SetTableSeparator(string separator)
156170
{
157171
TableSeparator = separator;
158172
}
173+
159174
/// <summary>
160175
/// 启动时检查分库信息是否完整
161176
/// </summary>
@@ -165,11 +180,13 @@ public void CheckShardingDataSourceMetadata()
165180
{
166181
throw new ShardingCoreException($"not found entity:{EntityType} configure");
167182
}
168-
if(ShardingDataSourceProperty==null)
183+
184+
if (ShardingDataSourceProperty == null)
169185
{
170186
throw new ShardingCoreException($"not found entity:{EntityType} configure sharding property");
171187
}
172188
}
189+
173190
/// <summary>
174191
/// 启动时检查分表信息是否完整
175192
/// </summary>
@@ -179,11 +196,13 @@ public void CheckShardingTableMetadata()
179196
{
180197
throw new ShardingCoreException($"not found entity:{EntityType} configure");
181198
}
199+
182200
if (ShardingTableProperty == null)
183201
{
184202
throw new ShardingCoreException($"not found entity:{EntityType} configure sharding property");
185203
}
186204
}
205+
187206
/// <summary>
188207
/// 启动时检查对象信息是否完整
189208
/// </summary>
@@ -195,6 +214,7 @@ public void CheckGenericMetadata()
195214
throw new ShardingCoreException($"not found entity:{EntityType} configure");
196215
}
197216
}
217+
198218
protected bool Equals(EntityMetadata other)
199219
{
200220
return Equals(EntityType, other.EntityType);
@@ -213,4 +233,4 @@ public override int GetHashCode()
213233
return (EntityType != null ? EntityType.GetHashCode() : 0);
214234
}
215235
}
216-
}
236+
}

src/ShardingCore/Core/ShardingConfigurations/ShardingConfigOptions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ namespace ShardingCore.Core.ShardingConfigurations
1212
/// </summary>
1313
public class ShardingConfigOptions
1414
{
15+
/// <summary>
16+
/// 是否使用代理模式
17+
/// </summary>
18+
public bool UseEntityFrameworkCoreProxies { get; set; } = false;
19+
1520
/// <summary>
1621
/// 写操作数据库后自动使用写库链接防止读库链接未同步无法查询到数据
1722
/// </summary>

src/ShardingCore/Core/TrackerManagers/ITrackerManager.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ public interface ITrackerManager
1818
bool AddDbContextModel(Type entityType,bool hasKey);
1919
bool EntityUseTrack(Type entityType);
2020
bool IsDbContextModel(Type entityType);
21+
Type TranslateEntityType(Type entityType);
2122
}
2223
}

src/ShardingCore/Core/TrackerManagers/TrackerManager.cs

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
using System.Linq;
55
using System.Text;
66
using Microsoft.EntityFrameworkCore;
7+
using ShardingCore.Core.ShardingConfigurations;
8+
using ShardingCore.Exceptions;
79
using ShardingCore.Sharding.Abstractions;
810

911
namespace ShardingCore.Core.TrackerManagers
@@ -15,9 +17,15 @@ namespace ShardingCore.Core.TrackerManagers
1517
* @Ver: 1.0
1618
* @Email: 326308290@qq.com
1719
*/
18-
public class TrackerManager: ITrackerManager
20+
public class TrackerManager : ITrackerManager
1921
{
20-
private readonly ConcurrentDictionary<Type,bool> _dbContextModels = new ();
22+
private readonly ShardingConfigOptions _shardingConfigOptions;
23+
private readonly ConcurrentDictionary<Type, bool> _dbContextModels = new();
24+
25+
public TrackerManager(ShardingConfigOptions shardingConfigOptions)
26+
{
27+
_shardingConfigOptions = shardingConfigOptions;
28+
}
2129

2230
public bool AddDbContextModel(Type entityType, bool hasKey)
2331
{
@@ -26,16 +34,66 @@ public bool AddDbContextModel(Type entityType, bool hasKey)
2634

2735
public bool EntityUseTrack(Type entityType)
2836
{
29-
if (!_dbContextModels.TryGetValue(entityType, out var hasKey))
37+
if (_dbContextModels.TryGetValue(entityType, out var hasKey))
38+
{
39+
return hasKey;
40+
}
41+
42+
if (_shardingConfigOptions.UseEntityFrameworkCoreProxies && entityType.BaseType != null)
3043
{
31-
return false;
44+
if (_dbContextModels.TryGetValue(entityType.BaseType, out hasKey))
45+
{
46+
return hasKey;
47+
}
3248
}
33-
return hasKey;
49+
50+
return false;
3451
}
3552

3653
public bool IsDbContextModel(Type entityType)
3754
{
38-
return _dbContextModels.ContainsKey(entityType);
55+
if (_dbContextModels.ContainsKey(entityType))
56+
{
57+
return true;
58+
}
59+
60+
if (_shardingConfigOptions.UseEntityFrameworkCoreProxies && entityType.BaseType != null)
61+
{
62+
return _dbContextModels.ContainsKey(entityType.BaseType);
63+
}
64+
65+
return false;
66+
}
67+
68+
public Type TranslateEntityType(Type entityType)
69+
{
70+
if (!_dbContextModels.ContainsKey(entityType))
71+
{
72+
if (_shardingConfigOptions.UseEntityFrameworkCoreProxies && entityType.BaseType != null)
73+
{
74+
if (_dbContextModels.ContainsKey(entityType.BaseType))
75+
{
76+
return entityType.BaseType;
77+
}
78+
}
79+
}
80+
81+
return entityType;
3982
}
83+
84+
//
85+
// public Type GetEntityType(Type entityType)
86+
// {
87+
// if (_dbContextModels.ContainsKey(entityType))
88+
// {
89+
// return entityType;
90+
// }
91+
//
92+
// if (entityType.BaseType != null&&_dbContextModels.ContainsKey(entityType.BaseType))
93+
// {
94+
// return entityType.BaseType;
95+
// }
96+
// throw new ShardingCoreInvalidOperationException($"entity type:{entityType.FullName} not found");
97+
// }
4098
}
41-
}
99+
}

src/ShardingCore/Core/VirtualRoutes/DataSourceRoutes/DataSourceRouteManager.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Generic;
44
using ShardingCore.Core.EntityMetadatas;
55
using ShardingCore.Core.ShardingEnumerableQueries;
6+
using ShardingCore.Core.TrackerManagers;
67
using ShardingCore.Core.VirtualDatabase.VirtualDataSources;
78
using ShardingCore.Core.VirtualRoutes.Abstractions;
89
using ShardingCore.Exceptions;

0 commit comments

Comments
 (0)