View Component无法获取数据

问题描述 投票:0回答:1

我在.net Core Web应用程序中执行View Component时遇到问题。我之前几次使用它没有问题,但这次它表现得很有趣。

在输出窗口中我可以清楚地看到Visual Studio正在尝试从数据库中查询数据,但是这个命令正在每秒多次执行无数个项目,直到我停止应用程序(结果当然不渲染) 。

另外我注意到一段时间后使用的内存大约是2.5GB而且还在增加。

谁知道什么是错的?任何帮助非常感谢!

看输出窗口::

    Application Insights Telemetry (unconfigured): {"name":"Microsoft.ApplicationInsights.Dev.Message","time":"2019-02-28T08:48:17.5125091Z","tags":{"ai.internal.nodeName":"V00X6036","ai.operation.name":"GET /DataTaxonomyTool/Index","ai.application.ver":"1.0.0.0","ai.internal.sdkVersion":"aspnet5c:2.1.1","ai.cloud.roleInstance":"V00X6036","ai.location.ip":"127.0.0.1","ai.operation.id":"63f680a6-4a03a53f2cc95492","ai.operation.parentId":"|63f680a6-4a03a53f2cc95492."},"data":{"baseType":"MessageData","baseData":{"ver":2,"message":"Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']\r\nSELECT [i].[Id], [i].[ObjectName], [i].[ObjectType]\r\nFROM [Items] AS [i]","severityLevel":"Information","properties":{"commandText":"SELECT [i].[Id], [i].[ObjectName], [i].[ObjectType]\r\nFROM [Items] AS [i]","CategoryName":"Microsoft.EntityFrameworkCore.Database.Command","DeveloperMode":"true","commandType":"Text","{OriginalFormat}":"Executed DbCommand ({elapsed}ms) [Parameters=[{parameters}], CommandType='{commandType}', CommandTimeout='{commandTimeout}']{newLine}{commandText}","elapsed":"1","AspNetCoreEnvironment":"Development","commandTimeout":"30"}}}}
    Microsoft.EntityFrameworkCore.Database.Command:Information: Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
    SELECT [i].[Id], [i].[ObjectName], [i].[ObjectType]
    FROM [Items] AS [i]

Application Insights Telemetry (unconfigured): {"name":"Microsoft.ApplicationInsights.Dev.Message","time":"2019-02-28T08:48:17.5378799Z","tags":{"ai.internal.nodeName":"V00X6036","ai.operation.name":"GET /DataTaxonomyTool/Index","ai.application.ver":"1.0.0.0","ai.internal.sdkVersion":"aspnet5c:2.1.1","ai.cloud.roleInstance":"V00X6036","ai.location.ip":"127.0.0.1","ai.operation.id":"63f680a6-4a03a53f2cc95492","ai.operation.parentId":"|63f680a6-4a03a53f2cc95492."},"data":{"baseType":"MessageData","baseData":{"ver":2,"message":"Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']\r\nSELECT [i].[Id], [i].[ObjectName], [i].[ObjectType]\r\nFROM [Items] AS [i]","severityLevel":"Information","properties":{"commandText":"SELECT [i].[Id], [i].[ObjectName], [i].[ObjectType]\r\nFROM [Items] AS [i]","CategoryName":"Microsoft.EntityFrameworkCore.Database.Command","DeveloperMode":"true","commandType":"Text","{OriginalFormat}":"Executed DbCommand ({elapsed}ms) [Parameters=[{parameters}], CommandType='{commandType}', CommandTimeout='{commandTimeout}']{newLine}{commandText}","elapsed":"1","AspNetCoreEnvironment":"Development","commandTimeout":"30"}}}}
    Microsoft.EntityFrameworkCore.Database.Command:Information: Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
    SELECT [i].[Id], [i].[ObjectName], [i].[ObjectType]
    FROM [Items] AS [i]

Application Insights Telemetry (unconfigured): {"name":"Microsoft.ApplicationInsights.Dev.Message","time":"2019-02-28T08:48:17.5612991Z","tags":{"ai.internal.nodeName":"V00X6036","ai.operation.name":"GET /DataTaxonomyTool/Index","ai.application.ver":"1.0.0.0","ai.internal.sdkVersion":"aspnet5c:2.1.1","ai.cloud.roleInstance":"V00X6036","ai.location.ip":"127.0.0.1","ai.operation.id":"63f680a6-4a03a53f2cc95492","ai.operation.parentId":"|63f680a6-4a03a53f2cc95492."},"data":{"baseType":"MessageData","baseData":{"ver":2,"message":"Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']\r\nSELECT [i].[Id], [i].[ObjectName], [i].[ObjectType]\r\nFROM [Items] AS [i]","severityLevel":"Information","properties":{"commandText":"SELECT [i].[Id], [i].[ObjectName], [i].[ObjectType]\r\nFROM [Items] AS [i]","CategoryName":"Microsoft.EntityFrameworkCore.Database.Command","DeveloperMode":"true","commandType":"Text","{OriginalFormat}":"Executed DbCommand ({elapsed}ms) [Parameters=[{parameters}], CommandType='{commandType}', CommandTimeout='{commandTimeout}']{newLine}{commandText}","elapsed":"1","AspNetCoreEnvironment":"Development","commandTimeout":"30"}}}}
    Microsoft.EntityFrameworkCore.Database.Command:Information: Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
    SELECT [i].[Id], [i].[ObjectName], [i].[ObjectType]
    FROM [Items] AS [i]

查看组件getMethod:

public class LayoutListsViewComponent : ViewComponent
    {

        DOOR.Core.Web.Models.ReportContext _context;

        public LayoutListsViewComponent(DOOR.Core.Web.Models.ReportContext context)
        {
            _context = context;
        }
        public IList<DOOR.Core.Web.Models.DataTaxonomy.Item> items { get; set; }
        public async Task<IViewComponentResult> InvokeAsync()
        {
            var items = await _context.Items
                .AsNoTracking()
                .ToListAsync();

            return View(items);
        }

    }

查看组件Default.cshtml:

@model IEnumerable<DOOR.Core.Web.Models.DataTaxonomy.Item>
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

@{
    Layout = "~/Pages/DataTaxonomyTool/Layout/LayoutTaxonomy.cshtml";
}

@for (char c = 'A'; c <= 'Z'; c++)
{
    <ul style="list-style:none">
        <li class="nav-item">
            <a class="nav-link" data-toggle="collapse" data-target="#@c.ToString().ToLower()">@c</a>
            <div id="@c.ToString().ToLower()" class="collapse">
                @foreach (var item in Model.OrderBy(x => x.ObjectName))
                {
                    @if (item.ObjectName.StartsWith(c.ToString().ToLower()))
                    {
                        <ul style="list-style:none">
                            <li>@item.ObjectName</li>
                        </ul>
                    }
                }
            </div>
        </li>
    </ul>
}

布局:

@addTagHelper*,DOOR.Core.Web

<!DOCTYPE html>
<html>
...
<vc:layout-lists></vc:layout-lists>
...
c# entity-framework asp.net-core
1个回答
0
投票

愚蠢我 - 在调用Default.cshtml时我忘了删除这部分:

@{
    Layout = "~/Pages/DataTaxonomyTool/Layout/LayoutTaxonomy.cshtml";
}

我使用此View Component以相同的布局获取一些数据。所以程序陷入无限循环。

热门问题
推荐问题
最新问题