1

我試圖用EF從我的數據庫中獲取數據。我有與它這樣相關的客戶端表干預:實體框架導航屬性

public partial class Client 
{ 
    public Client() 
    { 
     this.Interventions = new List<Intervention>(); 
    } 

    public int client_id { get; set; } 
    public string full_name { get; set; } 
    public string cgroup { get; set; } 
    public string nation { get; set; } 
    public virtual ICollection<Intervention> Interventions { get; set; } 
} 

public partial class Intervention 
{ 
    public int intervention_id { get; set; } 
    public int technician_id { get; set; } 
    public int client_id { get; set; } 
    public string type { get; set; } 
    public int done { get; set; } 
    public int year { get; set; } 
    public int month { get; set; } 
    public int week { get; set; } 
    public Nullable<int> avg_response_time { get; set; } 
    public int number_of_equip { get; set; } 
    public virtual Client Client { get; set; } 
    public virtual Technician Technician { get; set; } 
} 

我可以這樣得到的干預措施的列表:

public object Any(GetInterventions request) 
    { 
     List<Intervention> dbItems; 
     using (var context = new operationsContext()) 
     { 
      context.Configuration.LazyLoadingEnabled = false; 
      dbItems = context.Interventions.ToList(); 
      return new GetInterventionsResponse{ 
       interventions = dbItems 
      }; 
     } 
    } 

雖然,當我嘗試檢索相關的客戶端每次干預

dbItems = context.Interventions.Include("Client").ToList(); 

通過包含一個客戶端導航屬性我得到一個Visual Studio一個stackOverflowException。 我使用EF做了什麼錯誤,或者只是一般的不良編程問題?

在此先感謝

回答

2

的問題,通過引入上,我想對序列化JSON響應的類別和領域[DataContract]裝飾和[數據成員]解決。就像下面的例子:

using System.Runtime.Serialization; 
namespace OperationsAPI.Models 
{ 
    [DataContract] 
    public partial class Intervention 
    { 
     public int intervention_id { get; set; } 
     public int technician_id { get; set; } 
     public int client_id { get; set; } 
     public string type { get; set; } 
     public int done { get; set; } 
     public int year { get; set; } 
     [DataMember] 
     public int month { get; set; } 
     [DataMember] 
     public int week { get; set; } 
     [DataMember] 
     public Nullable<int> avg_response_time { get; set; } 
     public int number_of_equip { get; set; } 
     [DataMember] 
     public virtual Client Client { get; set; } 
     public virtual Technician Technician { get; set; } 
    } 
}