John John
John John

Reputation: 1

Checking if the data being retrieved from the database Eager loading OR lazy is loading

I have the following two models; AccountDefinition & SDOrganization where the relation is one-to-one.

public partial class AccountDefinition
    {
        public AccountDefinition()
        {
            this.AccountSiteMappings = new HashSet<AccountSiteMapping>();
        }

        public long ORG_ID { get; set; }


        public virtual SDOrganization SDOrganization { get; set; }
        public virtual ICollection<AccountSiteMapping> AccountSiteMappings { get; set; }}

&

public partial class SDOrganization
    {
        public SDOrganization()
        {
            this.AccountAttachments = new HashSet<AccountAttachment>();
            this.SDOrgUsers = new HashSet<SDOrgUser>();
            this.AaaContactInfoes = new HashSet<AaaContactInfo>();
            this.AaaUsers = new HashSet<AaaUser>();
            this.AaaPostalAddresses = new HashSet<AaaPostalAddress>();
        }

        public long ORG_ID { get; set; }
        public string NAME { get; set; }        


        public virtual AccountDefinition AccountDefinition { get; set; }
        public virtual SDOrgDetail SDOrgDetail { get; set; }
        public virtual SDOrgStatu SDOrgStatu { get; set; }
        public virtual ICollection<SDOrgUser> SDOrgUsers { get; set; }
        public virtual ICollection<AaaContactInfo> AaaContactInfoes { get; set; }
        public virtual ICollection<AaaUser> AaaUsers { get; set; }
        public virtual ICollection<AaaPostalAddress> AaaPostalAddresses { get; set; }
    }

On the Action method I have the following call to the repository:-

public ActionResult Index(string searchTerm=null)
        {
var accountdefinition = repository.FindAccountDefinition(searchTerm).ToList();

if (Request.IsAjaxRequest())
            {
                ViewBag.FromSearch = true;
                return PartialView("_CustomerTable",accountdefinition);
            }

            return View(accountdefinition);        
        }

And the repository the method looks as :-

public IQueryable<AccountDefinition> FindAccountDefinition(string q)
        {
            return from ad in entities.AccountDefinitions.Include(a => a.SDOrganization)
                   where (q == null || ad.ORG_NAME.ToUpper().StartsWith(q.ToUpper()) )
                   select ad;


        }

Finally on the view I got the following code (only part of the code):-

@model IEnumerable<TMS.Models.AccountDefinition>
//code goes here
<th>
            @Html.DisplayNameFor(model => model.SUPPORT_EMAIL)
        </th>
      <th>
            @Html.DisplayNameFor(model => model.Single().SDOrganization.DESCRIPTION)
        </th>
        <th></th>
    </tr>
//code goes here
@foreach (var item in Model) 
{
   <td class="center">
            @Html.DisplayFor(modelItem => item.SDOrganization.DESCRIPTION)

        </td>

I am assuming that since on the repository method I added the .include return from ad in

entities.AccountDefinitions.Include(a => a.SDOrganization)

So all the data regarding the accountdefinition and the SDorganization will be retrieved at once (eager loading). so I have the following two questions:-

  1. Will in my case the data retrieved by a single query to the database (Eager Loading).

  2. I am using SQL server 2008 r2. So how I can check the database queries to check how many query actually hit the database.

Upvotes: 0

Views: 100

Answers (1)

Oleksii Aza
Oleksii Aza

Reputation: 5398

  1. When you are using .Include() method you are initiating eager loading. So yes your data will be returned by single query.
  2. For checking it consider to use SQL Server Profiler or paid utility Entity Framework profiler. Also such utility as LinqPad could help you in query tracing

Also you need to be careful with repositories returning IQueryable because usage like this will execute several queries in a loop:

foreach(var accDef in repository.FindAccountDefinition(searchTerm))
{
    //get info from accDef
}

Upvotes: 1

Related Questions