表格不为空时的SqlNullException

时间:2019-04-16 18:13:15

标签: c# entity-framework

尝试使用实体框架从特定数据库中获取数据时遇到问题。我唯一要做的就是通过以下简单代码从数据库中获取列表:

namespace Script.Pages.CRUDScripts
{
    public class IndexModel : PageModel
    {
        private readonly Script.Bd _context;

        public IndexModel(Script.Bd context)
        {
            this._context = context;
        }

        public IList<Scripts> Scripts { get;set; }

        public async Task OnGetAsync()
        {
            var i = from s in _context.Scripts select s;

            Scripts = await i.ToListAsync();
        }
    }
}

类脚本:

            [Key]
            public int idScript { get; set; }

            [Display(Name = "Título")]
            [Required]
            public String titulo { get; set; }

            [Display(Name = "Autor")]
            [Required]
            public String autor { get; set; }

            [ForeignKey("Linguagem")]
            [Display(Name = "Linguagem")]
            public int idLinguagem { get; set; }

            [Display(Name = "Descrição")]
            [Required]
            public String dsScript { get; set; }

            [Display(Name = "Vínculos")]
            [Required]
            public String vinculosScript { get; set; }

            [Display(Name = "Versão")]
            [Required]
            public int versaoScript { get; set; }

            [Display(Name = "Referência Web")]
            public String refwebScript { get; set; }

            [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
            [Display(Name ="Data de criação")]
            public DateTime dtcriacaoScript { get; set; }

            [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
            [Display(Name ="Data último acesso")]
            public DateTime dtultimoacessoScript { get; set; }

            [Display(Name = "Corpo do script")]
            [Required]
            public String corpoScript { get; set; }

然后出现以下错误:

SqlNullValueException: Data is Null. This method or property cannot be called on Null values.
System.Data.SqlClient.SqlBuffer.get_String()
System.Data.SqlClient.SqlDataReader.GetString(int i)
lambda_method(Closure , DbDataReader )
Microsoft.EntityFrameworkCore.Storage.Internal.TypedRelationalValueBufferFactory.Create(DbDataReader dataReader)
Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable<T>+AsyncEnumerator.BufferlessMoveNext(DbContext _, bool buffer, CancellationToken cancellationToken)
Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsync<TState, TResult>(TState state, Func<DbContext, TState, CancellationToken, Task<TResult>> operation, Func<DbContext, TState, CancellationToken, Task<ExecutionResult<TResult>>> verifySucceeded, CancellationToken cancellationToken)
Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable<T>+AsyncEnumerator.MoveNext(CancellationToken cancellationToken)
System.Linq.AsyncEnumerable+SelectEnumerableAsyncIterator<TSource, TResult>.MoveNextCore(CancellationToken cancellationToken) in Select.cs
System.Linq.AsyncEnumerable+AsyncIterator<TSource>.MoveNext(CancellationToken cancellationToken) in AsyncIterator.cs
Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider+ExceptionInterceptor<T>+EnumeratorExceptionInterceptor.MoveNext(CancellationToken cancellationToken)
System.Linq.AsyncEnumerable.Aggregate_<TSource, TAccumulate, TResult>(IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator, Func<TAccumulate, TResult> resultSelector, CancellationToken cancellationToken) in Aggregate.cs
Script.Pages.CRUDScripts.IndexModel.OnGetAsync() in Index.cshtml.cs
+
            Scripts = await i.ToListAsync();
Microsoft.AspNetCore.Mvc.RazorPages.Internal.ExecutorFactory+NonGenericTaskHandlerMethod.Execute(object receiver, object[] arguments)
Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.InvokeHandlerMethodAsync()
Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.InvokeNextPageFilterAsync()
Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.Rethrow(PageHandlerExecutedContext context)
Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.InvokeInnerFilterAsync()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext)
Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext)
Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

这很奇怪,因为我可以成功地将数据添加到数据库中,并且还因为它以前运行良好。变量_context似乎有问题,并且只有在表脚本中进行搜索时,数据库中的其他表才能正常工作。感谢有人可以提供帮助。

0 个答案:

没有答案