109221793
109221793

Reputation: 16887

EF 4 not loading foreign key object

I am running an ASP.NET MVC 3 application (C#).

I recently made a foreign key in my database nullable. This probably isn't best practice however it is necessary for my scenario. So now when I display my report (ASPX View Engine), the values for the object which I have made nullable in my db will not display.

The data structure now (simplified) is as follows:

Inventory
________________
InventoryID int PK
ShopID FK int NOT NULL
ProductID FK int NULL

The only change to this is that ProductID used to be Not Nullable.

My ActionResult is as follows:

[HttpPost]
public ActionResult InventoryReport(FormCollection formVariables, InventoryReportRequest reportRequest)
        {
            ModelContainer ctn = new ModelContainer();

            var query = ctn.Inventory.Where(ic => !ic.Deleted && ic.ShopID == reportRequest.ShopID);

            if (reportRequest.ProductID.HasValue)
            {
                query = (from ic in query
                         where reportRequest.ProductID == ic.ProductID
                         select ic);
            }

            List<Inventory> checks = query.ToList();

            if (checks.Count > 0)
            {
                return View(checks);
            }
            else
            {
                return RedirectToAction("Index");
            }
        }

When I debug though this Action, I can see that the ProductID is being retrieved, however the Product object remains null. So when I want to display my results in my view -

<table class="normal" width="100%">
        <tr>
            <th>
                Date
            </th>
            <th>
                Shop
            </th>
            <th>
                **Product Name**
            </th>
            <th>
                **Product ID**
            </th>
            <th>
                Active
            </th>
        </tr>

    <% foreach (var item in Model) { %>
        <tr>
            <td>
                <%: Html.DisplayFor(modelItem => item.DateChecked) %>
            </td>
            <td>
                <%: Html.DisplayFor(modelItem => item.Shop.Name) %>
            </td>
            <td>
                **<%: Html.DisplayFor(modelItem => item.Product.Name) %>**
            </td>
            <td>
                **<%: Html.DisplayFor(modelItem => item.Product.ProductID) %>**
            </td>
            <td>
                <%: Html.DisplayFor(modelItem => item.Active) %>
            </td>
        </tr>
    <% } %> 

    </table>

The starred items appear blank.

Can any offer advice as to what I need to do to resolve this issue? If more information is required please just ask :)

Upvotes: 0

Views: 2019

Answers (1)

Gert Arnold
Gert Arnold

Reputation: 109089

Actually, it's more surprising that Product was loaded previously if you did not make any change to the query. For Product to be loaded you'd have to use Include, irrespective of the FK being nullable or not

ctn.Inventory.Include("Product").Where(...

Anyway, add the Include now and you'll be fine.

Upvotes: 4

Related Questions