2014-05-21 205 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

相关问题