fixed view entity

This commit is contained in:
xuejiaming 2023-03-22 11:09:27 +08:00
parent fd4835dbdf
commit 2965892839
6 changed files with 49 additions and 24 deletions

View File

@ -38,7 +38,7 @@ public class WeatherForecastController : ControllerBase
public async Task<IActionResult> Query()
{
var list =await _defaultDbContext.Set<OrderByHour>().ToListAsync();
var list =await _defaultDbContext.Set<OrderByHour>().AsNoTracking().ToListAsync();
return Ok(list);
}
public async Task<IActionResult> Insert()

View File

@ -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<SysUserMod>().Where(o => o.Name == "name_3")
.ExecuteDeleteAsync();
return Ok();
}
[HttpGet]
public async Task<IActionResult> Get9()
{
var resultx112331tt2 = await _defaultTableDbContext.Set<SysTest>().FromSqlRaw("select *from systest where id='3'").FirstOrDefaultAsync();

View File

@ -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<string> GetTails()
{
var shardingRuntimeContext = RouteShardingProvider.ApplicationServiceProvider.GetRequiredService<IShardingRuntimeContext>();
var beginTime = new DateTime(2020, 1, 1);
var endTime = new DateTime(2021, 12, 1);
var list = new List<string>(24);

View File

@ -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<EntityMetadata>();
_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<EntityMetadata>();
_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}]");
}
}
}

View File

@ -81,6 +81,8 @@ namespace ShardingCore.Core.EntityMetadatas
/// 逻辑表名
/// </summary>
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<PropertyInfo>();
IsSingleKey = PrimaryKeyProperties.Count == 1;
Schema = dbEntityType.GetEntityTypeSchema();
}

View File

@ -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
}
}
}