![]() ![]() Private static void LazyLoadingRelatedProperty() To briefly explain, lazy loading is where a related entity will be automatically loaded from the database the first time it is accessed, but not before. But I’ll comment on that at the end of this section. This is the trap that the development team, which I referred to above, fell into. That is to say, if you leave the default configuration as is, and don’t explicitly tell EF in your query that you want something other than lazy loading, then lazy loading is what you will get. ![]() The first type of access which we’ll look at is lazy loading. These then form part of the object graph which is loaded for that particular entity. Using an ORM such as EF, we end up with an object graph, which by its very nature, is not flattened: Figure 3Īll ORMs have their own conventions/API for loading related entities. That would result in a flattened resultset: Figure 2 In traditional SQL data retrieval, we would use a join or sub-query to access information about related entities. So, if we had a Software entity, a collection of SoftwareFiles entities are related to that Software entity. The following schema depicts an example of such a scenario: Figure 1Ī related entity (for the purposes of this article) is simply an entity that is related to another entity by way of a foreign key relationship. Let’s say someone had an MSDN subscription and wanted to manage the various software licences and files which they acquired through that. So what do I mean by related entities? First, I will introduce you to the schema of the database which we will use to demonstrate the principles in this article. But just so that readers are aware, I have used the expression-style of syntax (i.e., "dot notation"), as distinct from the SQL style. Note also that this is not an article about LINQ. But I hope to lay these things out clearly in this article in such a way as to enable people to ramp up quickly when beginning with EF. I won’t be going too deep, as people new to EF will find all of the various combinations of configuration and query-construction to be quite confusing at first. We will be covering concepts such as Lazy Loading, Eager Loading, Explicit Loading and proxies. So if this stuff could trip up a team of experienced developers, it’s worthy of a look. Finally, one of them ran up SQL Server Profiler to discover that there was something like 10,000 hits to the database every time a particular button was clicked on the page. And it was all to do with how they were loading related entities. Unfortunately, they didn’t understand how EF was behaving. ![]() In my first job, a team of experienced developers was working on an application which used EF as its Object Role Mapper (ORM). In the download code, the database will be created and seeded when you first run it. I have used EF6 for the code in this article, but it's applicable to all Code First versions. It is for people new to the entity framework, whether experienced or not as developers generally. This article is a beginner-level look at the topic of accessing related entities using the Entity Framework (EF). ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |