我有这个工作:
private readonly SuperContext _context;
public EmployeesController(SuperContext context)
{
_context = context;
}
public async Task<IActionResult> Index(string searchString)
{
var names = from m in db.Employee
select m;
if (!string.IsNullOrEmpty(searchString))
{
names = names.Where(s => s.FirstName.Contains(searchString)
|| s.LastName.Contains(searchString));
}
return View(await names.ToListAsync());
}
但现在我收到了这个错误:
fail: Microsoft.AspNetCore.Server.Kestrel[13]
Connection id "0HL3AAH6GJBIP": An unhandled exception was thrown by the
application.
System.NullReferenceException: Object reference not set to an instance of an
object.
at LRClinicSuperWP.Controllers.EmployeesController.<Index>d__3.MoveNext() in
H:\VSProjectP\src\Project\Controllers\EmployeesController.cs:line 35
第35行是return语句,因此IQueryable名称出现问题。数据库中有3个测试条目,而同一模型的其他查询在项目的其他控制器中工作,而不是这个。
什么会导致IQueryable不从数据库上下文返回数据?
更新
花了一些时间调试后,我发现问题不是IQueryable,而是模型。 db.Employee未从数据库获取数据。
'((Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<System.
IServiceProvider>)db.Employee).Instance' threw an exception of type
'System.NotImplementedException'
我仍在查看调试日志以找出原因,但如果有人能指出我正确的方向,我将不胜感激。