我有下一个问题,我有来自下面2个字段的简单MVC
public class Manager
{
public int Id { get; set; }
public String Name { get; set; }
public bool IsDeleted { get; set; }
public virtual List<Employee> EmplCol { get; set; }
public Manager()
{
this.EmplCol = new List<Employee>();
}
}
public class Employee
{
public int Id { get; set; }
public String Profeesion { get; set; }
public DateTime CreatedOn { get; set; }
public Employee() { }
}
和下一个实体框架上下文
public class EFContext : DbContext
{
public DbSet<Manager> Managers { get; set; }
}
将自动生成的WebApi 2
控制器添加到项目中Manager
为Model
且EFContext
为Context类后,我获得了序列化异常。在这种情况下,我该怎么做才能启用序列化?
答案 0 :(得分:0)
试试这个:
[DataContract]
public class Manager
{
[DataMember]
public int Id { get; set; }
[DataMember]
public String Name { get; set; }
[DataMember]
public bool IsDeleted { get; set; }
[JsonIgnore]
public virtual List<Employee> EmplCol { get; set; }
}
如果有效,我会编辑并解释原因,
答案 1 :(得分:0)
除了user2997779的答案:如果您收到“检测到属性的自引用循环...”异常消息,此页面有几种方法可以处理它:https://code.msdn.microsoft.com/Loop-Reference-handling-in-caaffaf7