延迟加载与实体框架5.0不工作的相关实体

本文关键字:实体 工作 框架 延迟加载 | 更新日期: 2023-09-27 18:04:29

我在我的项目中首先使用实体框架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>

所以当razor引擎渲染我的部分视图并执行以下命令

item.DiscountCard.CardName

我得到DicountCard为空,我无法理解是什么原因导致这是空的,任何人都可以帮助我这个。

延迟加载与实体框架5.0不工作的相关实体

问题是DiscountCard实际上 null。当您获取项目时,您正在使用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实例的可枚举对象。这样做的唯一作用是破坏这些项与上下文的关联,从而有效地消除了延迟加载任何内容的能力。

改成:

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