1

我有下一個問題,我簡單的MVC 2個clases下面序列化問題

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控制器後,我項目與ManagerModelEFContext作爲上下文類我得到序列化異常。在這種情況下我必須做些什麼來啓用序列化?

+0

我懷疑即使編譯爲'Manager'的構造函數也會嘗試設置'this.SheepCol',但是沒有調用字段或屬性'SheepCol' –

+0

@AndyNichols sory for這已經改變了它。當我嘗試這個'this.Configuration.ProxyCreationEnabled = false;'它修復了問題,但在這種情況下,我得到空'EmplCol' – abilash

+1

發佈您的代碼爲您的控制器,並請張貼完整的異常 – Alex

回答

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; } 
} 

如果一切正常,我將修改並解釋原因,