Reputation: 1
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:-
Will in my case the data retrieved by a single query to the database (Eager Loading).
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
Reputation: 5398
.Include()
method you are initiating eager loading. So yes your data will be returned by single query.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