繁体   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