如何加载类型为IEnumerable< T>的相关实体 [英] How do I load related entities of type IEnumerable<T>

查看:90
本文介绍了如何加载类型为IEnumerable< T>的相关实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试加载SingleOrDefault实体的相关实体,但是我收到以下异常:

I'm trying to load related entities of a SingleOrDefault entity, but I am getting the following exception:

IEnumerable类型的导航属性不是单个实现的ICollection类型

The navigation property of type IEnumerable is not a single implementation of type ICollection

我已经尝试这样做了几种方式,最终得到与上下文相同的每个查询上面的错误(我已经包括其他方式在注释)使用(var context = CustomObjectContextCreator.Create())
{
return context.Job。

I've tried doing this several ways and ultimately get the same error above for each query against the context (I've included the other ways in comments).

using(var context = CustomObjectContextCreator.Create())
        {
            return  context.Job.Include("Surveys").Include("SiteInfoes")
        .Where(r => r.Jobid == jobId).SingleOrDefault();

            //context.ContextOptions.LazyLoadingEnabled = false;
            //var Job = context.Job.Where(r => r.Jobid == jobId).SingleOrDefault();
            //context.LoadProperty(Job, "Surveys");
            //context.LoadProperty(Job, "SiteInfoes");

            //var Job = (from j in context.Job
            //                   .Include("Surveys")
            //                   .Include("SiteInfoes")
            //               select j).SingleOrDefault();

            //var Job = context.Job.Where(r => r.Jobid == jobId).SingleOrDefault();
            //var surveys = context.Surveys.Where(s => s.JobID == jobId);
            //var wellInfoes = context.SiteInfoes.Where(w => w.Jobid == jobId);
            //Job.Surveys = surveys.ToList();
            //Job.SiteInfoes = wellInfoes.ToList();

            //return Job;
        }

以下是我使用的POCO对象:

Here are the POCO objects I'm using:

    public class Job
    {
        public int? Jobid { get; set; }
        public string JobLocation { get; set; }
        public string JobName { get; set; }
        public virtual IEnumerable<Survey> Surveys { get; set; }
        public virtual IEnumerable<SiteInfo> SiteInfoes { get; set; }
    }

public class Survey
    {
        public int SurveyID { get; set; }
        public int? JobID { get; set; }
        public DateTime? DateTime { get; set; }
        public string Report { get; set; }
        public virtual Job Job { get; set; }
    }

public class SiteInfo
    {
      public int Jobid { get; set; }
      public string SiteLocation { get; set; }
      public virtual JobInfo JobInfo { get; set; }
    }

如何正确加载相关实体?

How do I properly load the related entities?

推荐答案

不支持IEnumerable< T> 作为导航集合的类型。您必须使用 ICollection< T> 或从其派生的其他界面(例如 IList< T> )或具体执行 ICollection< T> - 像列表< T> HashSet< T& / code>等。

IEnumerable<T> is not supported as a type for a navigation collection. You must use ICollection<T> or another interface derived from it (for example IList<T>) or a concrete implementation of ICollection<T> - like List<T>, HashSet<T>, etc.

这篇关于如何加载类型为IEnumerable&lt; T&gt;的相关实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆