1

我有一个名为 Employee 的实体,它有一个名为 Groups 的导航属性。我有一个名为 Group 的实体,它与 Employee 相关,并具有 Name 和 Description 字段

我有一个 EntityDataSource 来按在 GridView1 中选择的员工进行过滤,它具有包含组,它的定义如下:

<asp:EntityDataSource ID="GroupsByEmployeeSource" runat="server" ConnectionString="name=SafetyContext" DefaultContainerName="SafetyContext" EnableDelete="True" EnableFlattening="False" EnableInsert="True" EnableUpdate="True" EntitySetName="Employees" Include="Groups" Where="it.[EID] == @EmpID">
    <WhereParameters>
        <asp:ControlParameter Name="EmpID" Type="Int32" ControlID="GridView1" PropertyName="SelectedDataKey.Value" />
    </WhereParameters>
</asp:EntityDataSource>

GridView3 用于显示 Employee 所属的 Groups。我的设置是这样的:

<asp:GridView runat="server" ID="GridView3" DataSourceID="GroupsByEmployeeSource" AutoGenerateColumns="False">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Label ID="GroupsByEmployee" runat="server" Text='<%#Eval("Groups.Name") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

每次我在 GridView1 中选择员工时,都会引发以下异常:

System.Web.HttpException: DataBinding: 'System.Collections.Generic.HashSet`1[[SafetyTrackingConceptApplication.DAL.Group, SafetyTrackingConceptApplication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' does not contain a property with the name 'Name'.

我一定遗漏了一些东西,但实体组肯定有一个名为 Name 的属性。有谁知道这里有什么问题?

4

1 回答 1

0

Have/had the same problem. The relationship is returning the collection because of the fk (foreign key) relationship in that the Emp can be in multiple groups, which returns a collection as explained in the comment by AMasoud. So you can have the Groups.Count return the number of times the EmpID shows up the group table, but alas you cannot pinpoint a NAME b/c it will be returning the collection.

If the FK is one to one, or many to one (Not one to many) you will have the ability to pull the name.

于 2014-10-05T13:55:05.400 回答