[英]Entity Framework navigation property
I'm trying to use EF to get data from my database. 我正在尝试使用EF从数据库中获取数据。 I have a table Interventions that has a Client associated with it like this:
我有一个表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; }
}
I can get a list of interventions by doing this: 通过执行以下操作,我可以获得干预的列表:
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
};
}
}
Although, when I try to retrieve the client associated with each intervention 虽然,当我尝试检索与每个干预措施相关的客户时
dbItems = context.Interventions.Include("Client").ToList();
by including a Client navigation property I get a Visual Studio a stackOverflowException. 通过包含一个Client导航属性,我得到一个Visual Studio stackOverflowException。 Am I doing anything wrong using EF or is just a question of general bad programming?
我在使用EF时是否做错了什么,还是仅仅是一般的不良编程问题?
thanks in advance 提前致谢
The problem was solved by introducing decorations of [DataContract] and [DataMember] on the classes and fields that I wanted to serialize on the JSON response. 通过在要在JSON响应上序列化的类和字段上引入[DataContract]和[DataMember]修饰,解决了该问题。 Just like the example below:
就像下面的例子:
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.