正确使用实体框架中的导航属性

发布于 2024-12-28 18:18:34 字数 1362 浏览 2 评论 0原文

我正在制作一个表单,将新库存添加到我设计的库存跟踪数据库中。我已经完成了到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情何以堪。 2025-01-04 18:18:34

我没有看到你在哪里实例化你的公共字段设备。 (equipment in from Equipment in... 是另一个变量,即 LINQ 查询的范围变量。)查看您的代码,我期望出现 NullReferenceException< /code> 因为设备null

您应该有类似的内容:

using (MSSInvDBEntities myEntities = new MSSInvDBEntities())
{
    equipment = (from e in myEntities.Equipments
                 select e)
                .FirstOrDefault();
}

但这也会导致异常,因为您没有加载 equipment.EquipmentInventories 属性,并且延迟加载在您的 DisplayEquipmentData 方法中不起作用因为您已经处理了上下文(自动在 using 块的末尾)。延迟加载需要尚未释放的上下文。

在您的情况下,我会使用急切加载:

using (MSSInvDBEntities myEntities = new MSSInvDBEntities())
{
    equipment = (from e in myEntities.Equipments.Include("EquipmentInventories")
                 select e)
                .FirstOrDefault();
}

然后使用此查询立即加载导航属性,您可以安全地处置上下文。

I don't see where you instantiate your public field equipment. (equipment in from equipment in... is another variable, the range variable for the LINQ query.) Looking at your code I'd expect a NullReferenceException because equipment is null.

You should have something like:

using (MSSInvDBEntities myEntities = new MSSInvDBEntities())
{
    equipment = (from e in myEntities.Equipments
                 select e)
                .FirstOrDefault();
}

But this would cause an exception as well because you don't load the equipment.EquipmentInventories property and lazy loading won't work in your DisplayEquipmentData method because you already have disposed the context (automatically at the end of the using block). Lazy loading requires a context which isn't disposed yet.

In your case I would use eager loading though:

using (MSSInvDBEntities myEntities = new MSSInvDBEntities())
{
    equipment = (from e in myEntities.Equipments.Include("EquipmentInventories")
                 select e)
                .FirstOrDefault();
}

Then the navigation property is loaded at once with this query and you can safely dispose the context.

左耳近心 2025-01-04 18:18:34

斯劳玛说的话,或者……

private void DisplayEquipmentData() 
{ 
    ManuList.SelectedValue = equipment.equipmentManu; 
    ModelList.SelectedValue = equipment.equipmentModel; 
    if (!equipment.EquipmentInventoriesReference.IsLoaded)
        equipment.EquipmentInventoriesReference.Load();
    tboSerial.Text = equipment.EquipmentInventories.serialNumber; 

} 

What Slauma said, or...

private void DisplayEquipmentData() 
{ 
    ManuList.SelectedValue = equipment.equipmentManu; 
    ModelList.SelectedValue = equipment.equipmentModel; 
    if (!equipment.EquipmentInventoriesReference.IsLoaded)
        equipment.EquipmentInventoriesReference.Load();
    tboSerial.Text = equipment.EquipmentInventories.serialNumber; 

} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文