我有以下两种型号;帐户定义& SDOrganization,其中关系是一对一的。
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; }
}
在Action方法中,我对存储库进行了以下调用: -
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);
}
该方法的存储库如下: -
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;
}
最后在视图中我得到了以下代码(只是部分代码): -
@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>
我假设因为在存储库方法中我添加了.include
中的广告返回entities.AccountDefinitions.Include(a => a.SDOrganization)
因此,将立即检索有关帐户定义和SD组织的所有数据(急切加载)。 所以我有以下两个问题: -
在我的情况下,将通过单个查询检索到数据库的数据(Eager Loading)。
我正在使用SQL server 2008 r2。因此,我如何检查数据库查询以检查实际访问数据库的查询数量。
答案 0 :(得分:1)
.Include()
方法时,您正在开始急切加载。所以是的,您的数据将通过单个查询返回。此外,您需要注意返回IQueryable的存储库,因为这样的用法将在循环中执行多个查询:
foreach(var accDef in repository.FindAccountDefinition(searchTerm))
{
//get info from accDef
}