【问题标题】:Binding entities with foreign key to datalist将具有外键的实体绑定到数据列表
【发布时间】:2009-10-19 10:09:18
【问题描述】:

我有以下代码:

var devices = from d in ctx.Devices.Include("DeviceGroups")
    where d.DeviceEnabled == true
    select d;
dlTerminals.DataSource = devices;

在前端我执行以下操作:

<asp:DataList ID="dlTerminals" runat="server" DataKeyField="DeviceId" GridLines="None" RepeatColumns="2" RepeatDirection="Horizontal" Width="100%">
    <ItemTemplate>
            <%# Eval("DeviceGroups.GroupName")%>
    </ItemTemplate>
</asp:DataList>

但我收到以下错误:

不包含名为“GroupName”的属性。

【问题讨论】:

  • 我不确定。我认为这是 EF 的问题 :( 不是因为你的代码。

标签: asp.net entity-framework


【解决方案1】:

找到解决办法:

select new { d.DeviceId, d.MAC, d.DeviceType, d.LastConnectTime, d.DeviceGroups.FirstOrDefault().GroupName };

【讨论】:

    【解决方案2】:

    在开发 asp.net 时,扁平化数据可能是最好的解决方案。你也可以考虑在你的类中添加一个属性,比如

    public string DeviceGroupName
    {
        get
        {
            return this.DeviceGroups.FirstOrDefault();
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-28
      • 2018-05-27
      • 1970-01-01
      • 1970-01-01
      • 2018-11-22
      • 1970-01-01
      相关资源
      最近更新 更多