C# EF 4未加载外键对象
我正在运行一个ASP.NET MVC 3应用程序(C#) 我最近在数据库中设置了一个外键为空。这可能不是最佳实践,但对我的场景来说是必要的。因此,现在当我显示我的报告(ASPX视图引擎)时,我在数据库中设置为空的对象的值将不会显示 现在的数据结构(简化)如下:C# EF 4未加载外键对象,c#,database,asp.net-mvc-3,entity-framework,C#,Database,Asp.net Mvc 3,Entity Framework,我正在运行一个ASP.NET MVC 3应用程序(C#) 我最近在数据库中设置了一个外键为空。这可能不是最佳实践,但对我的场景来说是必要的。因此,现在当我显示我的报告(ASPX视图引擎)时,我在数据库中设置为空的对象的值将不会显示 现在的数据结构(简化)如下: Inventory ________________ InventoryID int PK ShopID FK int NOT NULL ProductID FK int NULL [HttpPost] public ActionRes
Inventory
________________
InventoryID int PK
ShopID FK int NOT NULL
ProductID FK int NULL
[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");
}
}
唯一的变化是ProductID过去不可为null
我的行动结果如下:
Inventory
________________
InventoryID int PK
ShopID FK int NOT NULL
ProductID FK int NULL
[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");
}
}
[HttpPost]
public ActionResult InventoryReport(FormCollection formVariables,InventoryReportRequest reportRequest)
{
ModelContainer ctn=新的ModelContainer();
var query=ctn.Inventory.Where(ic=>!ic.Deleted&&ic.ShopID==reportRequest.ShopID);
if(reportRequest.ProductID.HasValue)
{
查询=(来自查询中的ic)
其中reportRequest.ProductID==ic.ProductID
选择ic);
}
列表检查=query.ToList();
如果(checks.Count>0)
{
返回视图(检查);
}
其他的
{
返回操作(“索引”);
}
}
当我调试此操作时,我可以看到正在检索ProductID,但是Product对象仍然为null。因此,当我想在视图中显示结果时-
<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>
日期
商店
**产品名称**
**产品ID**
活跃的
项目。日期已勾选)%>
商品.商店.名称)%>
**项目.产品.名称)%>**
**item.Product.ProductID)%>**
项目(活动)%>
带星号的项目显示为空白
我需要做些什么来解决这个问题,你能给我一些建议吗?如果需要更多信息,请询问:)事实上,如果您没有对查询进行任何更改,则以前加载了
产品
,这更令人惊讶。对于要加载的产品
,您必须使用包含
,而不管FK是否为空
ctn.Inventory.Include("Product").Where(...
无论如何,现在添加
Include
,您就没事了。事实上,如果您没有对查询进行任何更改,那么以前加载了产品就更令人惊讶了。对于要加载的产品
,您必须使用包含
,而不管FK是否为空
ctn.Inventory.Include("Product").Where(...
无论如何,现在添加Include
就可以了。试试这个var query=ctn.Inventory..Include(“Shops”)。其中(ic=>!ic.Deleted&&ic.ShopID==reportRequest.ShopID)
这将在数据库中添加查询的适当更改。@saravanan感谢您的回复。我也试过了,但没有结果:格特·阿诺德指出,你是否在上下文中包含了这些产品。您是否碰巧调试了代码并在适当的关联中获取了值。请尝试此var query=ctn.Inventory..Include(“Shops”)。其中(ic=>!ic.Deleted&&ic.ShopID==reportRequest.ShopID)
这将在数据库中添加查询的适当更改。@saravanan感谢您的回复。我也试过了,但没有结果:格特·阿诺德指出,你是否在上下文中包含了这些产品。您是否碰巧调试了代码并在适当的关联中获取了值。嗨,Gert!谢谢你的回复。我已经修改了我的问题如上,但我仍然没有任何运气。我以前从来都不需要使用'Includes',这很奇怪,尽管没有对商店外键使用'Includes',所有商店属性仍然会加载。奇怪。。。我假设ModelContainer
是一个上下文,而Inventory
只不过是一个DbSet(或ObjectSet)!谢谢你的回复。我已经修改了我的问题如上,但我仍然没有任何运气。我以前从来都不需要使用'Includes',这很奇怪,尽管没有对商店外键使用'Includes',所有商店属性仍然会加载。奇怪。。。我假设ModelContainer
是一个上下文,而Inventory
只是一个DbSet(或ObjectSet)?