简体   繁体   中英

EF6, lazy loading not working as expected

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.

Try to define relationship between entities. It should work if your lazy loading has enabled.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM