0

次の 2 つのモデルがあります。関係が 1 対 1 である AccountDefinition & 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)

そのため、accountdefinition と SDorganization に関するすべてのデータが一度に取得されます (イーガー ロード)。だから私は次の2つの質問があります:-

  1. 私の場合、データはデータベースへの 1 回のクエリで取得されます (Eager Loading)。

  2. SQL Server 2008 r2 を使用しています。では、データベース クエリをチェックして、実際にデータベースにヒットしたクエリの数を確認する方法を教えてください。

4

1 に答える 1

1
  1. メソッドを使用しているときは、.Include()熱心な読み込みを開始しています。はい、データは単一のクエリで返されます。
  2. 確認するには、SQL Server Profilerまたは有料ユーティリティEntity Framework profilerの使用を検討してください。また、 LinqPadなどのユーティリティは、クエリのトレースに役立ちます

また、IQueryable を返すリポジトリにも注意する必要があります。このような使用法では、複数のクエリがループで実行されるためです。

foreach(var accDef in repository.FindAccountDefinition(searchTerm))
{
    //get info from accDef
}
于 2013-07-07T17:09:02.270 に答える