diff --git a/samples/Sample.AutoCreateIfPresent/Controllers/WeatherForecastController.cs b/samples/Sample.AutoCreateIfPresent/Controllers/WeatherForecastController.cs index 314397be..1426ed39 100644 --- a/samples/Sample.AutoCreateIfPresent/Controllers/WeatherForecastController.cs +++ b/samples/Sample.AutoCreateIfPresent/Controllers/WeatherForecastController.cs @@ -38,7 +38,7 @@ public class WeatherForecastController : ControllerBase public async Task Query() { - var list =await _defaultDbContext.Set().ToListAsync(); + var list =await _defaultDbContext.Set().AsNoTracking().ToListAsync(); return Ok(list); } public async Task Insert() diff --git a/samples/Sample.SqlServer/Controllers/ValuesController.cs b/samples/Sample.SqlServer/Controllers/ValuesController.cs index 146731c8..ad4c713a 100644 --- a/samples/Sample.SqlServer/Controllers/ValuesController.cs +++ b/samples/Sample.SqlServer/Controllers/ValuesController.cs @@ -11,11 +11,13 @@ using System.Diagnostics; using System.Linq; using System.Linq.Expressions; using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore.Metadata; using ShardingCore.Core.VirtualRoutes.TableRoutes; using ShardingCore.Extensions.ShardingQueryableExtensions; using ShardingCore.Core; using ShardingCore.Core.RuntimeContexts; using ShardingCore.Extensions.ShardingPageExtensions; +using ShardingCore.Helpers; using ShardingCore.Sharding.ReadWriteConfigurations.Abstractions; namespace Sample.SqlServer.Controllers @@ -405,14 +407,13 @@ namespace Sample.SqlServer.Controllers .ExecuteUpdateAsync( s => s.SetProperty(b => b.Age, b => b.Age + 1)); - + var xxx = await _defaultTableDbContext.Set().Where(o => o.Name == "name_3") + .ExecuteDeleteAsync(); return Ok(); } [HttpGet] public async Task Get9() - { - var resultx112331tt2 = await _defaultTableDbContext.Set().FromSqlRaw("select *from systest where id='3'").FirstOrDefaultAsync(); diff --git a/samples/Sample.SqlServer/Shardings/SysUserSalaryVirtualTableRoute.cs b/samples/Sample.SqlServer/Shardings/SysUserSalaryVirtualTableRoute.cs index 539f7620..8fafcc0a 100644 --- a/samples/Sample.SqlServer/Shardings/SysUserSalaryVirtualTableRoute.cs +++ b/samples/Sample.SqlServer/Shardings/SysUserSalaryVirtualTableRoute.cs @@ -1,8 +1,10 @@ using System; using System.Collections.Generic; using System.Linq.Expressions; +using Microsoft.Extensions.DependencyInjection; using Sample.SqlServer.Domain.Entities; using ShardingCore.Core.EntityMetadatas; +using ShardingCore.Core.RuntimeContexts; using ShardingCore.Core.VirtualRoutes; using ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions; using ShardingCore.Sharding.EntityQueryConfigurations; @@ -28,6 +30,7 @@ namespace Sample.SqlServer.Shardings public override List GetTails() { + var shardingRuntimeContext = RouteShardingProvider.ApplicationServiceProvider.GetRequiredService(); var beginTime = new DateTime(2020, 1, 1); var endTime = new DateTime(2021, 12, 1); var list = new List(24); diff --git a/src/ShardingCore/Core/EntityMetadatas/DefaultEntityMetadataManager.cs b/src/ShardingCore/Core/EntityMetadatas/DefaultEntityMetadataManager.cs index 67322c7d..e13ff8f7 100644 --- a/src/ShardingCore/Core/EntityMetadatas/DefaultEntityMetadataManager.cs +++ b/src/ShardingCore/Core/EntityMetadatas/DefaultEntityMetadataManager.cs @@ -144,27 +144,30 @@ namespace ShardingCore.Core.EntityMetadatas } } metadata.SetEntityModel(efEntityType); - if (string.IsNullOrWhiteSpace(metadata.LogicTableName)) + if (!metadata.IsView) { - throw new ShardingCoreInvalidOperationException( - $"init model error, cant get logic table name:[{metadata.LogicTableName}] from entity:[{efEntityType.ClrType}]"); - } - if (!_logicTableCaches.TryGetValue(metadata.LogicTableName, out var metadatas)) - { - metadatas = new List(); - _logicTableCaches.TryAdd(metadata.LogicTableName, metadatas); - } + if (string.IsNullOrWhiteSpace(metadata.LogicTableName)) + { + throw new ShardingCoreInvalidOperationException( + $"init model error, cant get logic table name:[{metadata.LogicTableName}] from entity:[{efEntityType.ClrType}]"); + } + if (!_logicTableCaches.TryGetValue(metadata.LogicTableName, out var metadatas)) + { + metadatas = new List(); + _logicTableCaches.TryAdd(metadata.LogicTableName, metadatas); + } - if (metadatas.All(o => o.EntityType != efEntityType.ClrType)) - { - metadatas.Add(metadata); - return true; - } - //添加完成后检查逻辑表对应的对象不可以存在两个以上的分片 - if (metadatas.Count > 1 && metadatas.Any(o => o.IsShardingTable() || o.IsShardingDataSource())) - { - throw new ShardingCoreInvalidOperationException( - $"cant add logic table name caches for metadata:[{metadata.LogicTableName}-{efEntityType.ClrType}]"); + if (metadatas.All(o => o.EntityType != efEntityType.ClrType)) + { + metadatas.Add(metadata); + return true; + } + //添加完成后检查逻辑表对应的对象不可以存在两个以上的分片 + if (metadatas.Count > 1 && metadatas.Any(o => o.IsShardingTable() || o.IsShardingDataSource())) + { + throw new ShardingCoreInvalidOperationException( + $"cant add logic table name caches for metadata:[{metadata.LogicTableName}-{efEntityType.ClrType}]"); + } } } diff --git a/src/ShardingCore/Core/EntityMetadatas/EntityMetadata.cs b/src/ShardingCore/Core/EntityMetadatas/EntityMetadata.cs index 040589c9..fc840ac2 100644 --- a/src/ShardingCore/Core/EntityMetadatas/EntityMetadata.cs +++ b/src/ShardingCore/Core/EntityMetadatas/EntityMetadata.cs @@ -81,6 +81,8 @@ namespace ShardingCore.Core.EntityMetadatas /// 逻辑表名 /// public string LogicTableName { get; private set; } + + public bool IsView { get; private set; } = false; /** * 对象表所属schema */ @@ -104,12 +106,17 @@ namespace ShardingCore.Core.EntityMetadatas public void SetEntityModel(IEntityType dbEntityType) { LogicTableName = dbEntityType.GetEntityTypeTableName(); + Schema = dbEntityType.GetEntityTypeSchema(); + if (string.IsNullOrWhiteSpace(LogicTableName)) + { + IsView = dbEntityType.GetEntityTypeIsView(); + } + QueryFilterExpression = dbEntityType.GetAnnotations().FirstOrDefault(o => o.Name == QueryFilter)?.Value as LambdaExpression; PrimaryKeyProperties = dbEntityType.FindPrimaryKey()?.Properties?.Select(o => o.PropertyInfo)?.ToList() ?? new List(); IsSingleKey = PrimaryKeyProperties.Count == 1; - Schema = dbEntityType.GetEntityTypeSchema(); } diff --git a/src/ShardingCore/Extensions/InternalExtensions/InternalIEntityTypeExtension.cs b/src/ShardingCore/Extensions/InternalExtensions/InternalIEntityTypeExtension.cs index 1e34224a..5ad8f9bc 100644 --- a/src/ShardingCore/Extensions/InternalExtensions/InternalIEntityTypeExtension.cs +++ b/src/ShardingCore/Extensions/InternalExtensions/InternalIEntityTypeExtension.cs @@ -35,5 +35,16 @@ namespace ShardingCore.Extensions.InternalExtensions #endif return tableName; } + + public static bool GetEntityTypeIsView(this IEntityType entityType) + { +#if !EFCORE2&&!EFCORE3 + return !string.IsNullOrWhiteSpace(entityType.GetViewName()); +#endif +#if EFCORE2 ||EFCORE3 + return false; +#endif + + } } }