JsonResult ASP.NET实体框架核心错误
问题描述:
我正在通过ASP.Net Core 1.0和EF Core创建应用程序。我使用AutoaMapper绘制了我的模型和视图模型。JsonResult ASP.NET实体框架核心错误
当我创建一个控制器,并调用它,我得到这个错误:
Error Number:208,State:1,Class:16 Exception thrown: 'System.Data.SqlClient.SqlException' in Microsoft.EntityFrameworkCore.dll CRAMSCore1.Models.CramsRepository:Error: Error getting complaints Microsoft.AspNetCore.Mvc.Formatters.Json.Internal.JsonResultExecutor:Information: Executing JsonResult, writing value.
当我检查我的SQL事件探查器上SSMS,我看到它与查询的数据库:
SQL:BatchCompleted SELECT [c].[COMP_ID], [c].[AddrCity], [c].[AddrState], [c].[AddrZip], [c].[Address], [c].[CRORoute_DT] FROM [Complaints] AS [c] Core .Net SqlClient Data Provider
我的仓库看上去很简单:
public IEnumerable<COMPLAINT> getAll()
{
try
{
return _context.Complaints
.ToList();
}
catch (Exception ex)
{
_logger.LogError("Error getting complaints", ex);
return null;
}
}
我的控制器看起来像:
[HttpGet("")]
public JsonResult Get()
{
var complaints = _repository.getAll();
var results = Mapper.Map<IEnumerable<ComplaintViewModel>>(complaints);
return Json(complaints);
}
答
运行该SQL手动给你什么?该错误似乎与您的JSONResult没有任何关系,但与通过EF从SQL中检索数据有关。
我也猜你的回报应该是
return Json(results);
它出现在我的DbContext不能正常工作,就像是当它是EF7。我将不得不调试... – epv