I am a LINQ2SQL fan, however, EF is also coming along. In my web application project I created various LINQ2SQL queries in different functions. Like:
Function A()
{
// LINQ2SQL QueryA accessing Database ABC
}
Function B()
{
// LINQ2SQL QueryB accessing Database ABC
}
Function C()
{
// LINQ2SQL QueryC accessing Database ABC
}
Function D()
{
// LINQ2SQL QueryD accessing Database ABC
}
Function E()
{
// LINQ2SQL QueryE a开发者_JAVA百科ccessing Database ABC
}
As you can see, my app is not efficient at all. I want to make this efficient by querying my DB only once and then fetching from it in different functions. What should be the better implementation for this?
Thanks!
You will need to keep the same context throughout the lifetime of your webrequest. Using the same context, will allow you to benifit from the cache. It will also allow you to save the time required to build the context on each request throughout the lifetime of your webrequest.
You will also need to load the data using eager loading.
ei. eager loading, is loading data using the "Include()" method. this will load all specified related data.
精彩评论