开发者

Entity Framework Include and Navigation Property

开发者 https://www.devze.com 2023-03-17 21:36 出处:网络
I use Asp.net and EF 4. In my model I have two Entities: CmsGroupsTypes wich has a navigational property called CmsContents to Entity CmsContents.

I use Asp.net and EF 4.

In my model I have two Entities: CmsGroupsTypes wich has a navigational property called CmsContents to Entity CmsContents.

I'm using an EntityDataSource control together with a GridView.

I need return CmsGroupsTypes but filtering theme using the Navigational Property and QueryStringParameter.

With the following code I receive an error:

'ContentId' is not a member of 'Transient.collection[CmsModel.CmsContent(Nullable=True,DefaultValue=)]'. To extract a property of a collection 开发者_JAVA技巧element, use a subquery to iterate over the collection

<asp:EntityDataSource ID="EntityDataSource1" runat="server"
    ConnectionString="name=CmsConnectionStringEntityDataModel" DefaultContainerName="CmsConnectionStringEntityDataModel"
    EnableFlattening="False" EntitySetName="CmsGroupsTypes" Include="it.CmsContents.ContentId"
    Where="it.CmsContents.ContentId == ContentId">
    <WhereParameters>
        <asp:QueryStringParameter Name="ContentId" QueryStringField="ContentId" DbType="Int32" />
    </WhereParameters>
</asp:EntityDataSource>

Any idea what I'm doing wrong?

I have an equivalent version in LINQ and it is working but I have to implementing directily on the EntityDataSource Control.

      // Get ContentId from Query String.
        int myContentId = Convert.ToInt32(ContentIdFromUrl);
        // Find all GroupsType for a specific Content.
        var myGroupsTypesList = from g in context.CmsGroupsTypes
                                where g.CmsContents.Any(x => x.ContentId == myContentId)
                                select g;


Quick guess: Include takes the name of a navigation property, so instead of:

Include="it.CmsContents.ContentId"

shouldn't it be

Include="it.CmsContents"

?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号