[英]Getting a list of entities from a table leads to 500 error - Entity Framework
I'm building an SPA on Angular2 (front-end) and back-end web API 2 with OWIN and Identity for authentication and Ninject as a container for DI. 我正在Angular2(前端)和后端Web API 2上构建SPA,其中OWIN和Identity用于身份验证,Ninject用作DI的容器。
I want to return a list of Managers as json response. 我想以json响应的形式返回Managers列表。 And also its related ContactDetail data.
还有它的相关ContactDetail数据。 They are related as one-to-one in my database.
它们在我的数据库中一对一关联。 But I'm getting a 500 error as response.
但我收到500错误作为回应。 Why am I getting this error?
为什么我收到此错误?
Manager class 经理班
public partial class Manager
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ContactDetail ContactDetail { get; set; }
}
ContactDetail class ContactDetail类
public partial class ContactDetail
{
public int Id { get; set; }
public string FirstName { get; set; }
public string SecondName { get; set; }
public string LastName { get; set; }
public virtual Manager Manager { get; set; }
}
Context 上下文
public partial class LightCRMEntities : DbContext
{
public LightCRMEntities()
: base("name=LightCRMEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<ContactDetail> ContactDetails { get; set; }
public virtual DbSet<Manager> Managers { get; set; }
}
API controller API控制器
[Authorize(Roles = "Admin, Moderators")]
public class ManagerAPIController : ApiController
{
private IManagerRepository _iManagerRepository;
public ManagerAPIController(IManagerRepository iManagerRepository)
{
_iManagerRepository = iManagerRepository;
}
[HttpGet]
[Route("api/AllManagers")]
public IHttpActionResult GetAllManagers()
{
var managers = _iManagerRepository.Managers;
return Json(managers);
}
}
Repository: 库:
public class EFManagerRepository : IManagerRepository
{
public IEnumerable<Manager> Managers
{
get
{
using (LightCRMEntities context = new LightCRMEntities())
{
return context.Managers.ToList();
}
}
}
}
- ContactDetail '((System.Data.Entity.DynamicProxies.Manager_63305B0F1D8DAA0805603D889C6E3A5114E3050AEE610FE775000D2270697C1F)(new System.Collections.Generic.Mscorlib_CollectionDebugView(managers).Items[0])).ContactDetail' threw an exception of type 'System.ObjectDisposedException' LightCRM.ContactDetail {System.ObjectDisposedException}
ContactDetail'((System.Data.Entity.DynamicProxies.Manager_63305B0F1D8DAA0805603D889C6E3A5114E3050AEE610FE775000D2270697C1F)(new System.Collections.Generic.Mscorlib_CollectionDebugView(managers).Items [0]))。ContactDetail'抛出类型为'System.ObjectDisposedException'的异常LightCRM.ContactDetail { System.ObjectDisposedException}
Response: 响应:
I'm receiving 500 (Internal server error) 我收到500(内部服务器错误)
ExceptionMessage : "Error getting value from 'ContactDetail' on 'System.Data.Entity.DynamicProxies.Manager_63305B0F1D8DAA0805603D889C6E3A5114E3050AEE610FE775000D2270697C1F'."
ExceptionMessage:“在'System.Data.Entity.DynamicProxies.Manager_63305B0F1D8DAA0805603D889C6E3A5114E3050AEE610FE775000D2270697C1F''从'ContactDetail'获取值时出错。” ExceptionType : "Newtonsoft.Json.JsonSerializationException"
ExceptionType:“Newtonsoft.Json.JsonSerializationException”
Screenshot of a breakpoint: Though the error does not appear on this point - I can see that one of the related entities ContactDetail - has some problem to be loaded 断点的屏幕截图:虽然此时没有出现错误 - 我可以看到其中一个相关实体ContactDetail - 有一些问题需要加载
I hardly understand the meaning of: Configuration.ProxyCreationEnabled = false;
我几乎不明白其含义:
Configuration.ProxyCreationEnabled = false;
but if I add it to the constructor of the Context: 但是如果我将它添加到Context的构造函数中:
public partial class LightCRMEntities : DbContext
{
public LightCRMEntities()
: base("name=LightCRMEntities")
{
Configuration.ProxyCreationEnabled = false;
}
// other code
}
Then I get 200 ok and a json object in console: 然后我在控制台中得到200 ok和一个json对象:
but it dosn't load the related object - it has a NULL value - ContactDetail : null 但它不加载相关对象 - 它有一个NULL值 - ContactDetail:null
SQL profiler screenshot for this request: 此请求的SQL profiler屏幕截图:
Screenshot of data in the database: 数据库中数据的屏幕截图:
Thanks to @JWeezy, @DavidG and other guys from the comment section of the original post the solution was found: 感谢@JWeezy,@ DavidG和原帖的评论部分中的其他人,找到了解决方案:
Modifications needed: 需要修改:
disable lazy loading in the Context 在Context中禁用延迟加载
Context 上下文
public partial class LightCRMEntities : DbContext
{
public LightCRMEntities()
: base("name=LightCRMEntities")
{
Configuration.LazyLoadingEnabled = false;
}
// other code
}
Used eager loading in the repository 在存储库中使用了急切加载
public class EFManagerRepository : IManagerRepository
{
public IEnumerable<Manager> Managers
{
get
{
using (LightCRMEntities context = new LightCRMEntities())
{
return context.Managers.Include("ContactDetail").ToList();
}
}
}
}
In the ContactDetail class added [JsonIgnore] attribute to the virtual property 在ContactDetail类中,将[JsonIgnore]属性添加到虚拟属性中
public partial class ContactDetail
{
//other properties
[JsonIgnore]
public virtual Manager Manager { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.