Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
527 views
in Technique[技术] by (71.8m points)

c# - How do I load related entities of type IEnumerable<T>

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

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

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...