Entity Framework Eager Load Not Returning Data, Lazy Load Does

c# entity-framework lazy-loading poco

Question

Code First EF5 is what I'm using, and I have an object with a collection declared asvirtual (Slow loaded) When used, this returns information. But I do want it eager loaded. I've taken outvirtual but now it always returns from the property signaturenull The query is not even run by EF. data, can someone assist?

Edit: I am aware of.include() Simply said, I would prefer to achieve it without using virtual property.

Objects

User ([Key] Resource object's Id, which is the parent of the person class)

namespace Entities
{
    [Table("Users")]
    public class User : Person
    {

    [Required]
    public ICollection<Role> Roles { get; set; } 

    }
}

Role:

namespace Entities
{
    public class Role
    {
        [Key]
        public string Id { get; set; }

        public virtual ICollection<User> Users { get; set; } 
    }
}
1
16
3/25/2014 10:04:28 AM

Accepted Answer

This misunderstanding is typical. In contrast to lazy loading, you deliberately perform the loading yourself (for example, through eager loading usingInclude ).

Therefore, if you disable lazy loading in any way, such as by uninstalling thevirtual One of them is a modifier. — the behavior changes to zero loading rather than quickly loading.

Consider the scenario when EF eagerly loads everything that is not designated for lazy loading. By running one straightforward query, you run the danger of loading half the database!

If, after reading the above, you decide that you still want eager loading to be the default for a navigation property, you can use the code no way.

39
5/23/2017 12:32:02 PM

Popular Answer

To compel the eager loading of the ICollections into your entities, you must utilize the include method. You may find this link useful: http://msdn.microsoft.com/en-us/data/jj574232.aspx



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