2014-05-21 199 views
0

我使用實體框架5在我的項目中首先使用數據庫。我相信在外鍵虛擬字段中延遲加載值時存在一些問題,並且我嘗試了各種方法,但目前爲止我無法解決此問題。延遲加載相關實體不與實體框架5.0工作

下面是詳細信息:

我有一個CustomerDiscountCardController指數方法如下:

public ActionResult Index(String searchTerm = null, int page = 1) 
     { 
      var model = CustomerDiscountCardPagedList(searchTerm, page); 


      ViewBag.CustomerID = new SelectList(db.Customers.OrderBy(c => c.FirstName), "CustomerID", "FirstName"); 
      if (Request.IsAjaxRequest()) 
      { 
       return PartialView("_CustomerDiscountCard", model); 
      } 
      return View(model); 
     } 

     private IPagedList<CustomerDiscountCard> CustomerDiscountCardPagedList(String searchTerm, int page) 
     { 
      var model = db.CustomerDiscountCards.Include(d => d.DiscountCard). 
       OrderBy(c => c.DiscountCard.CardName). 
       Where(c => (c.DiscountCard.CardName.Contains(searchTerm))). 
       Select(c => new 
       { 
        DiscountCardID = c.DiscountCardID, 
        CustomerID = c.CustomerID, 
        IssueDate = c.IssueDate, 
        ExpiryDate = c.ExpiryDate, 
        CustomerDiscountCardID = c.CustomerDiscountCardID 
       } 
       ).ToList().Select(c => new CustomerDiscountCard 
       { 
        DiscountCardID = c.DiscountCardID, 
        CustomerID = c.CustomerID, 
        IssueDate = c.IssueDate, 
        ExpiryDate = c.ExpiryDate, 
        CustomerDiscountCardID = c.CustomerDiscountCardID 

       }).ToPagedList(page, 4); 
      return model; 
     } 

我的數據上下文類看起來如下:

public OnlineFoodOrderingEntities2() 
     : base("name=OnlineFoodOrderingEntities2") 
    { 
     this.Configuration.LazyLoadingEnabled = true; 
     this.Configuration.ProxyCreationEnabled = true; 
    } 
    //Entities class code.... 

} 

我的自動生成的CustomerDiscountCard類看起來如下所示:

public partial class CustomerDiscountCard 
{ 
    public int CustomerDiscountCardID { get; set; } 
    public Nullable<int> DiscountCardID { get; set; } 
    public Nullable<int> CustomerID { get; set; } 
    public Nullable<System.DateTime> IssueDate { get; set; } 
    public Nullable<System.DateTime> ExpiryDate { get; set; } 
    public virtual Customer Customer { get; set; } 
    public virtual DiscountCard DiscountCard { get; set; } 
} 

現在的問題是當我的索引方法被調用Ajax請求返回_CustomerDiscountCard

佈局的代碼的局部佈局是如下:

@using PagedList; 
@using PagedList.Mvc; 
@using OnlineFoodOrderingMVC.Models; 
@model IPagedList<CustomerDiscountCard> 

<div id="targetCustomerDiscountCardList"> 

    <div class="pagedList" data-ofo-target="#targetCustomerDiscountCardList"> 

     @Html.PagedListPager(Model, page => Url.Action("Index", "CustomerDiscountCard", new { page }), 
     PagedListRenderOptions.MinimalWithItemCountText) 
    </div> 
    <div style="clear:both"></div> 
    <div> 
     <table class="entity_record_table"> 
      <tr class="entity_record_table_header"> 
       <th class="entity_record_table_header_item_text"> 
        CustomerDiscountCards 
       </th> 
       <th class="entity_record_table_header_link_text"> 
        Edit 
       </th> 
       <th class="entity_record_table_header_link_text"> 
        View 
       </th> 
       <th class="entity_record_table_header_link_text"> 
        Delete 
       </th> 
      </tr> 
      @{ 
       String[] rowDisplay = { "entity_record_row_display1", "entity_record_row_display2" }; 
       String currentClass = "entity_record_row_display1"; 
       int count = 0; 
      } 

      @foreach (var item in Model) 
      { 

       count++; 
       currentClass = rowDisplay[count % 2]; 

       <tr class="@(currentClass)"> 
        <td class="entity_record_table_item"> 
         @Html.DisplayFor(modelItem => item.DiscountCard.CardName) 
        </td> 
        <td class="entity_record_table_link"> 
         @Ajax.ActionLink(" ", "Edit", new { id = item.CustomerDiscountCardID }, 
         new AjaxOptions 
          { 
           HttpMethod = "get", 
           InsertionMode = InsertionMode.Replace, 
           UpdateTargetId = "targetDiv" 
          } 
         , new { @class = "entity_record_edit" }) 
        </td> 
        <td class="entity_record_table_link"> 
         @Ajax.ActionLink(" ", "Details", new { id = item.CustomerDiscountCardID }, 
           new AjaxOptions 
           { 
            HttpMethod = "get", 
            InsertionMode = InsertionMode.Replace, 
            UpdateTargetId = "targetCustomerDiscountCardList" 
           } 
         , new { @class = "entity_record_view" }) 
        </td> 
        <td class="entity_record_table_link"> 
         @Ajax.ActionLink(" ", "Delete", new { id = item.CustomerDiscountCardID }, 
           new AjaxOptions 
           { 
            HttpMethod = "get", 
            InsertionMode = InsertionMode.Replace, 
            UpdateTargetId = "targetCustomerDiscountCardList" 
           } 
         , new { @class = "entity_record_delete" }) 
        </td> 
       </tr> 

      } 

     </table> 
    </div> 

</div> 

所以當剃刀發動機渲染此局部視圖礦山和執行下列指令

item.DiscountCard.CardName 

我得到DicountCard爲空,我不能夠明白是什麼原因造成這是空任何一個可以請幫我 這個。 非常感謝。

+0

對於一個在你的剃鬚刀@ Html.DisplayFor你正在做的'@ Html.DisplayFor(modelItem => item.DiscountCard。CardName)'請注意,您將模型作爲** modelItem **進行調用,但是您將**項目**將**項目**更改爲** modelItem ** – dklingman

+0

我剛剛添加了代碼的快照,以便查看器將理解我想要實現的,如果你想要發佈完整的剃刀視圖代碼,如下所示 – user3649270

+0

@dklingman:OP在「foreach」塊中執行此操作。正確的表達式*是*'modelItem => item'。 –

回答

1

問題是,DiscountCard真的爲空。當你獲取的物品,你使用Select創建一個新的類出值的子集:

  Select(c => new 
      { 
       DiscountCardID = c.DiscountCardID, 
       CustomerID = c.CustomerID, 
       IssueDate = c.IssueDate, 
       ExpiryDate = c.ExpiryDate, 
       CustomerDiscountCardID = c.CustomerDiscountCardID 
      } 
      ).ToList().Select(c => new CustomerDiscountCard 
      { 
       DiscountCardID = c.DiscountCardID, 
       CustomerID = c.CustomerID, 
       IssueDate = c.IssueDate, 
       ExpiryDate = c.ExpiryDate, 
       CustomerDiscountCardID = c.CustomerDiscountCardID 

      }) 

首先,你在做什麼是重複的。沒有理由選擇匿名對象,將其轉換爲列表,然後選擇具有相同數據的真實類實例。只需直接選擇的類的實例,即:

  Select(c => new CustomerDiscountCard 
      { 
       DiscountCardID = c.DiscountCardID, 
       CustomerID = c.CustomerID, 
       IssueDate = c.IssueDate, 
       ExpiryDate = c.ExpiryDate, 
       CustomerDiscountCardID = c.CustomerDiscountCardID 

      }) 

其次,你永遠不設置DiscountCard的值,只有DiscountCardId。當你創建一個像這樣的新實例時,它不是而是附加到你的上下文,並且沒有辦法獲取或檢索這個相關的值。這給我們帶來了...

第三,你爲什麼選擇任何東西?首先使用可枚舉的CustomerDiscountCard,然後選擇一個可枚舉的新實例CustomerDiscountCard。這樣做的唯一的事情是打破這些項目的附件到你的上下文,有效地殺死任何懶惰加載任何東西的能力。

UPDATE

只是將其更改爲:

var model = db.CustomerDiscountCards.Include(d => d.DiscountCard). 
      OrderBy(c => c.DiscountCard.CardName). 
      Where(c => (c.DiscountCard.CardName.Contains(searchTerm))). 
      ToPagedList(page, 4); 
+0

那麼請你能告訴我一個方法,我怎麼可能實現它與分頁列表? – user3649270

+0

非常感謝您的回答,但是您在發佈更新之前發佈的內容我嘗試瞭解所發佈內容的解決方法,並且最終得到了類似於您的解決方案,並且它還解決了我的問題,因爲您仍然百萬分,謝謝您,並且我無法發佈答案直到8小時,因爲我是新成員 – user3649270

相關問題