Click here to Skip to main content
15,889,266 members
Articles / Programming Languages / C#
Tip/Trick

How to force eager fetch on entity framework especially with inheritance

Rate me:
Please Sign up or sign in to vote.
4.33/5 (5 votes)
4 Jun 2017CPOL2 min read 9.8K   2   3
Lazy loading is a nice feature but sometimes it is more efficient to eager fetch. However entity framework does not provide an easy way to do it.

Introduction

Entity Framework is now massively used by developer. Indeed it offer nice features and a builtin ORM (Microsoft provided product) to play with database.

In ORM world there is 3 things important to analyse before using it :

  • Database driver support
  • Transparency (Intrusive/coupling) : mapping, tracking & API
  • Query strategy : performance & reliability

Concerning query strategy, lazy loading & fetching have a important impact on performance.

For example NHibernate allows full fetching, fetching by cluster defined by level hierarchy, fetching batch on lazy trigger and fetching included in query (HQL, Criteria, linq). Unfortunately, activate cluster fetching & batch fetching in NHibernate cause a major memory issue.

What about Entity Framework fetching options?

In entity framework, there is only one fetching strategy included directly in query materialized by an extension method on IQueryable named 'Include'.

https://msdn.microsoft.com/en-us/library/jj574232(v=vs.113).aspx

public class Product
{
    [Key]
    public int Id { get, set; }
}

public class Author
{
    [Key]
    public int Id { get, set; }
}
public class Book : Product
{
    public Author Author { get; set; }
}​​​​
dbContext.Set<Product>().Include(...) // cannot fetch Author for book!

Fetching with Entity Framework

There is no native implementation to full eager fetch in entity framework. Additionally, there is no advanced fetching strategy. Unfortunately fetch is always manual and non transparent. Developer have to use the 'Include' method which is not a business method but a data method contrary to IQueryable native extension method that describe search business.

I am sure that Entity Framework will implements all kind of fetch strategy later but at this time, we have to compose with it.

In fetching mode offer by entity it is hard to fetch property if you work with polymorphic objects. Include does not have a syntax to work with derived class. It means that in some case you just cannot fetch your known data.

Reliability

A second major error is to be able to disable lazy loading by removing virtual modifier from property declaration. If it is the case, Entity Framework will let property to default value. If the business is not vigilant about missing property or missing fetching declaration, data can be easily corrupted.

That's why I recommand to never disable lazy-loading event if you don't need it. Indeed, if you envolve your code, you can take risk t corrupt data if you disable lazy-loading. 

Fetching Workarround

After some research and test, I found a workarround to fetch data of derived type. You have to use a projection to anonymous type with to flat all derived types :

dbContext.Set<Product>()
    .Include(product => new { Product = product, Author = (product is Book) ? (product as Book).Author : null })
    .AsEnumerable()
    .Select(projection => projection.Product);

 

Conclusion

Working with complex structure and entity framework can be hard. I recommand to stay with a simple structure if you use entity framework or to pray for new features. If you use entity framework because you don't have choice (your architect impose you the framework for example), try to keep lazy-loading always active (for reliability) and wrap queryable to replace expression at runtime if you want to auto-fetch because manual fetch work in most of time and perform good but represents a double design issue : data consideration in business and difficulty to maintain fetch up to date when envolve business code.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
France France
After working in electronics, I fall in love with software development which is a real passion for me, particulary architecture concerns for productivity.

Comments and Discussions

 
QuestionCould provide helpful information Pin
Member 93194475-Jun-17 8:49
Member 93194475-Jun-17 8:49 
AnswerRe: Could provide helpful information Pin
Puresharper5-Jun-17 8:59
professionalPuresharper5-Jun-17 8:59 
GeneralRe: Could provide helpful information Pin
Member 93194475-Jun-17 13:53
Member 93194475-Jun-17 13:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.