So, I have the following model classes in my DbContext:

Every time I create a list of LoanApplication objects, I do something like this:
var context = new MyContext(); var applications = context.LoanApplications.Where(d => d.PropertyThatIWantToFilter = localVariable);
This returns an IQueryable, which is then converted to a ViewModel, as happens when the controller method is called:
var vm = applications.Select(d => new LoanApplicationViewModel(d));
Inside the LoanApplicationViewModel constructor LoanApplicationViewModel I accept an entity object and perform the appropriate mapping. The fact is that since the Solicitors collection is a navigation property, a call is made to the database every time a new presentation model is created. The average number of lawyers per application is two, so this means that if I create a table listing the last 10 applications, the application makes about 18-20 trips to the database.
I thought there should be a better way to get this collection, so I modified my initial request to look forward to downloading the collection as follows:
var applications = context.LoanApplications.Include("Solicitors").Where...
Although this reduced the number of database calls by only one, the query was much slower, about 50% slower.
The database is hosted on SQL Azure, and we applied Transient Fault Handling, but I want to reduce the number of calls made to the database, without compromising response performance.
What is the best practice here?
c # asp.net-mvc entity-framework code-first azure-sql-database
amhed Apr 03 '13 at 3:29 2013-04-03 03:29
source share