EF6, lazy loading not working as expected

.net c# entity-framework entity-framework-6 linq

Question

With EF 6,Lazy Loading Enabled in place toTrue inside the model. Here is an illustration of my issue:

var agent = context.AgentDetail.Where(a => a.Agent.GroupCode == "1234");

That operation will provide 5 outcomes. if I proceed to run (for the purpose of testing only)

var code = agent.FirstOrDefault().Agent.GroupCode;

As a result, I get a null reference exceptionAgent is null .

What I have are these:

    public partial class AgentDetail : Entity<int>
    {
        public Nullable<System.DateTime> Date { get; set; }
        public string Name { get; set; }
        public decimal Balance { get; set; }
        ...

        public virtual Agent Agent { get; set; }
    }

    public partial class Agent : Entity<int>
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public Agent()
        {
            this.AgentAspNetUsers = new HashSet<AgentAspNetUsers>();
            this.AgentDetail = new HashSet<AgentDetail>();
        }

        public string GroupCode { get; set; }
        ...

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<AgentAspNetUsers> AgentAspNetUsers { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<AgentDetail> AgentDetail { get; set; }
    }

Then how did it provide 5 results for my initial search? I'm not sure what's wrong, so any assistance would be much appreciated.

1
3
2/6/2017 12:16:01 PM

Popular Answer

ZZZ_tmp
0
2/6/2017 12:51:18 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow