Navigation properties not loading propertly

asp.net-mvc c# entity-framework entity-framework-6

Question

My situation is as follows:

public class ApplicationDbContext: IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection")
    {
        this.Configuration.LazyLoadingEnabled = true;
    }

    //DbSet properties
}

Lazy loading is therefore enabled.

The class I'm in is:

public class Home
{
    private ICollection<Slide> _slides;

    [Key]
    [Required]
    public string Name { get; set; }

    [ForeignKey("Header")]
    public int? HeaderID { get; set; }

    //Navigation properties
    public ICollection<Slide> Slides
    {
        get { return _slides ?? (_slides = new List<Slide>()); }
        set { _slides = value; }
    }

    public Content Header { get; set; }
}

Keep in mind that both navigational elementsHeader and Slides are without usedvirtual crucial phrase. If we don't use itvirtual properties with the keyword should load quickly.

However, after I receive myHome both of my navigational properties are an entity from the database.null however, propertyHeaderID has worth).
Despite switching tothis.Configuration.LazyLoadingEnabled = false; properties not loaded yet still therenull .

Here is how I use the repository pattern to get my data from the database:

public static Home GetHomeComponent(
    this IRepositoryAsync<Home> repository)
{
   var result = repository
       .Query()
       .Select()
       .First();
   return result;
}

I used to address my issue withInclude properties:

public static Home GetHomeComponent(
    this IRepositoryAsync<Home> repository)
{
   var result = repository
       .Query()
       .Include(x => x.Header)
       .Include(x=>x.Slides)
       .Select()
       .First();
   return result;
}

However, I don't find it convenient (since I have too much navigation properties to load).

I thus ask:
I use don't.virtual keyword, but why are my navigational properties taking so long to load?
Or am I doing something incorrectly? Is there a different approach to loading my navigation properties that doesn't includeInclude ?

1
9
11/22/2018 5:12:24 AM

Accepted Answer

If you do not utilize thevirtual This simply indicates that if you try to access a non-virtual property, a message will appear instead of the non-virtual property being loaded from the database.Null .

It does not imply that you will immediately have all of the properties of the entities filled up.Slides You must use in your code for E.G. Include() is an example of eager loading because it allows you to load a property before using it.

You can create a generic function that uses the parameters it receives to fill in the necessary properties ( using params ) For further information, go here:

EntityFramework Load all navigation properties eagerly

12
5/23/2017 10:30:59 AM


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