簡體   English   中英

實體框架導航屬性

[英]Entity Framework navigation property

我正在嘗試使用EF從數據庫中獲取數據。 我有一個表Interventions,具有與之相關的客戶,如下所示:

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();

通過包含一個Client導航屬性,我得到一個Visual Studio stackOverflowException。 我在使用EF時是否做錯了什么,還是僅僅是一般的不良編程問題?

提前致謝

通過在要在JSON響應上序列化的類和字段上引入[DataContract]和[DataMember]修飾,解決了該問題。 就像下面的例子:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM