簡體   English   中英

延遲加載不使用Entity Framework 5.0的相關實體

[英]Lazy loading of related entities not working with Entity Framework 5.0

我在項目中首先使用數據庫使用實體框架5。 我認為外鍵虛擬字段中的值的延遲加載存在一些問題,我已經嘗試了所有可能的方法,但是到目前為止,我仍然無法解決該問題。

詳細信息如下:

我有一個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為空,我不明白是什么原因導致此為空,任何人都可以幫助我解決這個問題。 非常感謝。

問題在於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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM