返回WebService.NET中具有子级和父级的实体框架对象



我正在研究.NET Web服务和DB实体框架。

我有这个模式为我的项目字典模式

我需要返回包含所有描述、图像、示例、共轭和字典的条目,所以我制作了这个网络方法

 [WebMethod]
    public SEP_Entry RetrieveEntry(int Entry_id)
    {
        using (var db = new DictionaryDBEntities())
        {
            var row = (from R in db.SEP_Entry where R.Entry_id == Entry_id select R).SingleOrDefault();
            return row;
        }
    }

当我运行这个方法时,它只返回Entry,没有它的子项(Description、Example、Image)或父项(Dictionary)。

<SEP_Entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
<EntityKey>
<EntitySetName>SEP_Entry</EntitySetName>
<EntityContainerName>DictionaryDBEntities</EntityContainerName>
<EntityKeyValues>
<EntityKeyMember>
<Key>Entry_id</Key>
<Value xsi:type="xsd:int">14</Value>
</EntityKeyMember>
</EntityKeyValues>
</EntityKey>
<Entry_id>14</Entry_id>
<Copy_Entry_id>13</Copy_Entry_id>
<Dic_id>1</Dic_id>
<Pic_id xsi:nil="true"/>
<Entry_root>(ب ي د)</Entry_root>
<Entry_stem>اباد</Entry_stem>
<Entry_word>أَبادَ</Entry_word>
<SEP_DictionaryReference>
<EntityKey>
<EntitySetName>SEP_Dictionary</EntitySetName>
<EntityContainerName>DictionaryDBEntities</EntityContainerName>
<EntityKeyValues>
<EntityKeyMember>
<Key>Dic_id</Key>
<Value xsi:type="xsd:int">1</Value>
</EntityKeyMember>
</EntityKeyValues>
</EntityKey>
</SEP_DictionaryReference>
<SEP_PictureReference/>
</SEP_Entry>

如何返回具有所有子项(描述、示例、图像)和父项(字典)的对象(条目)?

如果我理解正确,您只需要急切地加载所需的相关对象。默认情况下,EF只抓取父对象,然后根据需要延迟加载子对象。你可以越过它,告诉它热切地装载所有的孩子(在第一次跑步时)。

也许可以尝试这样的方法,即使用.Include()来急切地加载您的孩子。包括文档

using (var db = new DictionaryDBEntities())
{
    var row = (from R in db.SEP_Entry where R.Entry_id == Entry_id select R).Include("Description").Include("Example").Include("Image").SingleOrDefault();
    return row;
}

最新更新