EF6, lazy loading not working as expected

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

Question

Using EF 6, Lazy Loading Enabled is set to True in the model. Here's an example of my problem:

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

Running that will return 5 results. If after that I run (for the purpose of testing only)

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

I get a null reference exception because Agent is null.

Here are my entities:

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

How could it give me 5 results in the first query, then? I can't figure out what's wrong here, any help would be appreciated.

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

Popular Answer

From Requirements for Creating POCO Proxies

  1. A custom data class must be declared with public access.
  2. A custom data class must not be sealed
  3. A custom data class must not be abstract
  4. A custom data class must have a public or protected constructor that does not have parameters. Use a protected constructor without parameters if you want the CreateObject method to be used to create a proxy for the POCO entity. Calling the CreateObject method does not guarantee the creation of the proxy: the POCO class must follow the other requirements that are described in this topic.
  5. The class cannot implement the IEntityWithChangeTracker or IEntityWithRelationships interfaces because the proxy classes implement these interfaces.
  6. The ProxyCreationEnabled option must be set to true.
  7. Each navigation property must be declared as public, virtual (Overridable in Visual Basic), and not sealed (NotOverridable in Visual Basic) get accessor. The navigation property defined in the custom data class must have a corresponding navigation property in the conceptual model. For more information, see Loading Related POCO Entities.

Check this points on your classes. In your pasted code AgentDetail havent public/protected constructor.

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