Entity Framework - 使用 context.[tablename] 返回空数据

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

我有这个代码

    [Route("Home/AddComponentMenuCanvas")]
    public async Task<JsonResult> AddComponentMenuCanvas()
    {
        List<object> strHtml = new List<object>();
        using (var ctx = new Models.webexoContext())
        {
            List<Models.TlTemplateLayout> tl = ctx.TlTemplateLayout.Where(x => x.TlTctId == 11).ToList();
            Newtonsoft.Json.Linq.JObject js;
            System.Xml.Linq.XDocument xdoc;
            foreach (Models.TlTemplateLayout t in tl)
            {
                string guid = Guid.NewGuid().ToString().Replace("-", "");
                js = Newtonsoft.Json.Linq.JObject.Parse(t.TlJson);
                xdoc = System.Xml.Linq.XDocument.Parse(t.TlHtml);
                var objArr = new object[] { ComponentBuild.Constants.ComponentMenuHtml(xdoc, js, 100, ref guid, true, false), t.TlId, };
                strHtml.Add(objArr);
            }

            var omt = new ObjectMenuTemplate
            {
                tl = tl,
                html = strHtml
            };

            string partialViewHtml = await Constants.RenderViewToStringAsync(this, "AddMenu", omt);
            return Json(new { data = partialViewHtml });
        }

    }

但是这一行的脚本错误:

List<Models.TlTemplateLayout> tl = ctx.TlTemplateLayout.Where(x => x.TlTctId == 11).ToList();

并输出此错误:

        An unhandled exception occurred while processing the request.
    SqlNullValueException: Data is Null.This method or property cannot be called on Null values.
    Microsoft.Data.SqlClient.SqlBuffer.get_String()

    Stack Query Cookies Headers Routing
    SqlNullValueException: Data is Null.This method or property cannot be called on Null values.
    Microsoft.Data.SqlClient.SqlBuffer.get_String()
    lambda_method375(Closure, QueryContext, DbDataReader, ResultContext, SingleQueryResultCoordinator)
    Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable < T > +Enumerator.MoveNext()
    System.Collections.Generic.List < T > ..ctor(IEnumerable < T > collection)
    System.Linq.Enumerable.ToList<TSource>(IEnumerable < TSource > source)
    WebAmbe.Controllers.HomeController.AddComponentMenuCanvas() in HomeController.cs
    +
                    var tl = ctx.TlTemplateLayout.Where(x => x.TlTctId == 11).ToList();
    lambda_method370(Closure, object)
    Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor + TaskOfActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, object controller, object[] arguments)
    System.Threading.Tasks.ValueTask<TResult>.get_Result()
    System.Runtime.CompilerServices.ValueTaskAwaiter<TResult>.GetResult()
    Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.< InvokeActionMethodAsync > g__Awaited | 12_0(ControllerActionInvoker invoker, ValueTask < IActionResult > actionResultValueTask)
    Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.< InvokeNextActionFilterAsync > g__Awaited | 10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
    Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
    Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
    Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
    Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.< InvokeNextResourceFilter > g__Awaited | 25_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
    Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
    Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
    Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
    Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.< InvokeAsync > g__Awaited | 17_0(ResourceInvoker invoker, Task task, IDisposable scope)
    Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.< InvokeAsync > g__Awaited | 17_0(ResourceInvoker invoker, Task task, IDisposable scope)
    Microsoft.AspNetCore.Routing.EndpointMiddleware.< Invoke > g__AwaitRequestTask | 6_0(Endpoint endpoint, Task requestTask, ILogger logger)
    Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
    Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
    Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
    Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

我使用 .net 6.0,不确定为什么会发生这种情况,因为在 .net 3.1 上工作正常。

c# entity-framework
© www.soinside.com 2019 - 2024. All rights reserved.