正确使用实体框架中的导航属性
我正在制作一个表单,将新库存添加到我设计的库存跟踪数据库中。我已经完成了到 EF 的映射,并且正在使用 LINQ to EF 来查询数据。
设备表有一个导航属性EquipmentInventories
。考虑以下代码片段:
public partial class Content_AddInventoryItems : System.Web.UI.Page
{
public Equipment equipment;
protected void Page_Load(object sender, EventArgs e)
{
using (MSSInvDBEntities myEntities = new MSSInvDBEntities())
{
var manuPop = (from equipment in myEntities.Equipments
select equipment.equipmentManu).Distinct();
ManuList.DataSource = manuPop;
ManuList.DataBind();
}
using (MSSInvDBEntities myEntities = new MSSInvDBEntities())
{
var modelPop = from equipment in myEntities.Equipments
select equipment.equipmentModel;
ModelList.DataSource = modelPop;
ModelList.DataBind();
}
}
private void DisplayEquipmentData()
{
ManuList.SelectedValue = equipment.equipmentManu;
ModelList.SelectedValue = equipment.equipmentModel;
tboSerial.Text = equipment.EquipmentInventories.serialNumber;
}
}
但是,当我尝试使用 EquipmentInventories
导航属性引用 EquipmentInventories
对象的 serialNumber
属性时,我不断收到错误equipment
对象的。
有什么想法我错在哪里吗?
I am working on a form to add new inventory into an inventory tracking database I designed. I have done a mapping to EF and I am using LINQ to EF to query data.
The equipment table has a navigation property EquipmentInventories
. Consider the following snippet of code:
public partial class Content_AddInventoryItems : System.Web.UI.Page
{
public Equipment equipment;
protected void Page_Load(object sender, EventArgs e)
{
using (MSSInvDBEntities myEntities = new MSSInvDBEntities())
{
var manuPop = (from equipment in myEntities.Equipments
select equipment.equipmentManu).Distinct();
ManuList.DataSource = manuPop;
ManuList.DataBind();
}
using (MSSInvDBEntities myEntities = new MSSInvDBEntities())
{
var modelPop = from equipment in myEntities.Equipments
select equipment.equipmentModel;
ModelList.DataSource = modelPop;
ModelList.DataBind();
}
}
private void DisplayEquipmentData()
{
ManuList.SelectedValue = equipment.equipmentManu;
ModelList.SelectedValue = equipment.equipmentModel;
tboSerial.Text = equipment.EquipmentInventories.serialNumber;
}
}
However I keep getting errors when I try to reference the serialNumber
property of the EquipmentInventories
object using the EquipmentInventories
navigation property of the equipment
object.
Any ideas where I went wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我没有看到你在哪里实例化你的公共字段
设备
。 (equipment
infrom Equipment in...
是另一个变量,即 LINQ 查询的范围变量。)查看您的代码,我期望出现NullReferenceException< /code> 因为
设备
为null
。您应该有类似的内容:
但这也会导致异常,因为您没有加载
equipment.EquipmentInventories
属性,并且延迟加载在您的DisplayEquipmentData
方法中不起作用因为您已经处理了上下文(自动在using
块的末尾)。延迟加载需要尚未释放的上下文。在您的情况下,我会使用急切加载:
然后使用此查询立即加载导航属性,您可以安全地处置上下文。
I don't see where you instantiate your public field
equipment
. (equipment
infrom equipment in...
is another variable, the range variable for the LINQ query.) Looking at your code I'd expect aNullReferenceException
becauseequipment
isnull
.You should have something like:
But this would cause an exception as well because you don't load the
equipment.EquipmentInventories
property and lazy loading won't work in yourDisplayEquipmentData
method because you already have disposed the context (automatically at the end of theusing
block). Lazy loading requires a context which isn't disposed yet.In your case I would use eager loading though:
Then the navigation property is loaded at once with this query and you can safely dispose the context.
斯劳玛说的话,或者……
What Slauma said, or...