Six Myths about Ontologies: The Basics of Formal Ontology
Entity framework advanced
1. Entity Framework
Session #3 : advanced topics & best practices
By: Usama Nada.
Lead Software Engineer
2. Agenda
• Eager Loading vs Lazy Loading
• Asynchronous query and save
• Optimistic Concurrency using RowVersion
• Best Practices in Entity Framework Usage
3. Eager Loading vs Lazy Loading
• Lazy Loading:
Related objects (child objects) are not loaded automatically with its parent object until
they are requested.
• Eager Loading:
Forcing Child related objects to load with its parent
4. Asynchronous query and save
• Non blocking calls to the data layer is a great way to increase the scalability
of your system.
• Frees up the worker thread to accept another request while work is being
done in the background
5. Optimistic Concurrency
• The pessimistic approach is a user thinking "I'm sure someone will try to
update this row at the same time as I will, so I better ask the database server
to not let anyone touch it until I'm done with it.“
• The optimistic approach is a user thinking "I'm sure it's all good and there
will be no conflicts, so I'm just going to remember this row to double-check
later, but I'll let the database server do whatever it wants with the row."
6. Best Practices
• Never Use Singleton DbContext
• Update to the latest version using NuGet Package manager
• If you face performance issue of complex queries, you have the ability to use stored
procedures.
• Minimize roundtrips to database
• Avoid fetching all the fields if not required.
• When object cache has very large amount of entities, it causes performance issues.
• Disabling change tracking to reduce state management overhead
• Split Large models for better designer and source control performance
• Choose appropriate Collection for data manipulation IEnumerableVS IQueryable,
7. General Best Practices
• Limit your result set using reasonable limits using paging. Never Load all table
entities (unless you have good reason for that)
• Cache Expensive result sets:
• Take long time in execution
• Short execution time bug get called too many times like lookups
• Embrace Asynchronous Programming techniques
• Use Fast Serializers
• What Else ??
8. Resources
• Performance Considerations for Entity Framework 4, 5, and 6:
http://msdn.microsoft.com/en-us/data/hh949853.aspx
• Why you should use async tasks in .NET 4.5 and Entity Framework 6:
http://www.itworld.com/development/405462/why-you-should-use-async-tasks-net-
45-and-entity-framework-6
• DbContext in Singleton: http://www.britishdeveloper.co.uk/2011/03/dont-use-singleton-
datacontexts-entity.html
Notas del editor
You need to ensure that you never execute two async tasks in parallel using the same data context. You also need to make sure you eager load / include any relational data your object might have to fetch from your async call.
The performance benefits are too great to ignore, so while it may add a bit of complexity to your software
asynchronous processing in the .NET Framework to avoid blocking when it’s waiting for data to be returned has daunted many a developer, especially when using disconnected apps or remote databases.
Singleton:
Performance issues when the object cache has many entities
DbContext is lightweight and is not expensive to create
DbContext will not be disposed and keep eating your memory.
In order to take advantage of connection pooling (and you should), database connections should be as short lived as possible. Create, use, then immediately destroy.
The data in will be out of sync with the database. you could encounter unexpected and serious problems.
http://www.britishdeveloper.co.uk/2011/03/dont-use-singleton-datacontexts-entity.html
http://msdn.microsoft.com/en-us/library/system.data.linq.datacontext.aspx#0cb87d0d-0981-48f1-ab1a-6fc0fdfa5e9f_c
https://www.linkedin.com/groups/Entity-Framework-DbContext-Lifetime-Management-43315.S.5818868403309395968
Update to latest version:
Default assembly added by VS2013 is not the latest missing performance enhancements