【问题标题】:DataBinding: Generic.HashSet`1 does not contain a property with the nameDataBinding:Generic.HashSet`1 不包含具有名称的属性
【发布时间】:2013-05-15 22:09:54
【问题描述】:

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

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

<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'.

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

【问题讨论】:

  • 查看运行时尝试绑定集合而不是实体的异常。 Groups 是一个集合,因此没有名为 Name 的属性。您想绑定 Groups 类型的集合元素
  • 那么我该如何进行呢?我读过这个:stackoverflow.com/questions/2284374/…,我正在做那里的工作。这里有什么区别?

标签: c# asp.net entity-framework gridview entitydatasource


【解决方案1】:

有/遇到过同样的问题。由于 fk(外键)关系,该关系正在返回集合,因为 Emp 可以位于多个组中,这将返回一个集合,如 AMasoud 的评论中所述。因此,您可以让 Groups.Count 返回 EmpID 显示组表的次数,但遗憾的是,您无法确定 NAME b/c 它将返回集合。

如果 FK 是一对一或多对一(不是一对多),您将有能力提取名称。

【讨论】:

    猜你喜欢
    • 2011-10-07
    • 2012-02-06
    • 2012-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多