本文介绍了带有 EntityDataSource 的一个 ASP.NET GridView 中两个相关数据库表的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个 SQL Server 表,主键 (PK) 和外键 (FK) 链接这两个表:

I have two SQL Server tables with Primary Keys (PK) and a Foreign Key (FK) linking the two tables:

1) Table "Order"

OrderID, int, PK
AddressID, int, FK
...

2) Table "Address"

AddressID, int, PK
City, nvarchar(50)
...

然后我从这两个表中创建了一个 (ADO.NET) 实体数据模型.现在在我的 (ASP.NET) 网页上,我放置了一个带有 EntityDataSource 的 GridView.在我的 GridView 中,我想显示两列:

Then I've created an (ADO.NET) Entity Data Model out of those two tables.Now on my (ASP.NET) webpage I put a GridView with an EntityDataSource. In my GridView I want to display two columns:

  • 订单编号
  • 城市(属于该订单并由 AddressID 键链接)

我该怎么做?我的问题是:当我配置实体数据源时,我可以选择一个EntitySetName"订单"或地址",但不能同时选择两者,我也不能选择任何类型的关系.如果我选择订单"作为 EntitySetName然后在 GridView 我可以添加列

How can I do that? My problem is: When I configure the Entity Data Source I can select an "EntitySetName" which can beeither "Order" or "Address" but not both, nor can I select any kind of relationship. If I select "Order" as EntitySetNamethen in the GridView I can add the columns

  • 订单号
  • 地址
  • 地址.地址ID

添加地址"列会显示空单元格.添加OrderID"和Address.AddressID"会显示预期的 ID.但是我怎么能将相关地址的城市"添加到我的GridView?

Adding the column "Address" displays empty cells. Adding "OrderID" and "Address.AddressID" displays the expected IDs. But how can Iadd the "City" of the related address to my GridView?

提前感谢您的帮助!

澄清:

实体框架已经创建了一个类订单"和一个类地址"对应于数据库表.Order"类引用了Address"对象作为导航属性,对应Address和Order表的1-n关系.

The Entity Framework has created a class "Order" and a class "Address" corresponding to the database tables. The class "Order" has a reference to an "Address" object as a navigation property, corresponding to the 1-n relationship between Address and Order table.

基本上我想在我的 GridView 中有一列显示 Order.Address.City.我试图将一个带有Address.City"的绑定字段作为数据字段添加到 GridView,但它导致运行时错误(没有这样的属性......").

Basically I want to have a column in my GridView which displays Order.Address.City. I have tried to add a bound field with "Address.City" as data field to the GridView but it results in a runtime error ("no such property...").

推荐答案

好吧,太多小时后我自己找到了解决方案:

OK, much too many hours later I found the solution myself:

选项 1:

可以在 EntityDataSource 中使用 select 属性,该属性允许从多个相关实体/数据库表中创建数据的任意投影(在我的情况下:来自 Order 实体的 OrderID 和来自地址实体)

It is possible to use the select property in the EntityDataSource which allows to create arbitrary projections of data from several related entities/database tables (in my case: OrderID from Order entity and City from theAddress entity)

缺点:在 EntityDataSource 中使用 select 使得在 GridView 中无法使用 Insert、Update 和 Delete!

Drawback: Using select in the EntityDataSource makes using Insert, Update and Delete in the GridView impossible!

选项 2:

EntityDataSource 必须具有 include 属性以包含相关地址属性以及查询订单.标记如下所示:

The EntityDataSource must have the include property to include the related address property along with the queried orders. The markup looks likes this:

<asp:EntityDataSource ID="EntityDataSourceOrders" runat="server"
    ConnectionString="name=MyEntitiesContext"
    DefaultContainerName="MyEntitiesContext"
    EntitySetName="Order" Include="Address"
    EnableDelete="True" EnableInsert="True"
    EnableUpdate="True">
</asp:EntityDataSource>

那么GridView的columns集合可以有这样的模板字段:

Then the columns collection of the GridView can have a template field like this:

<asp:TemplateField HeaderText="City" >
  <ItemTemplate>
    <asp:Label ID="LabelCity" runat="server" Text='<%# Eval("Address.City") %>'>
    </asp:Label>
  </ItemTemplate>
</asp:TemplateField>

这里 Eval 很重要.绑定 不起作用.还使用 BoundField 作为列...

Here Eval is important. Bind does not work. Also using a BoundField as a column...

<asp:BoundField DataField="Address.City" HeaderText="City" />

...不可能.所以在 GridView 中编辑 City 是不可能的(这是有道理的,因为它是属于另一个表的相关字段,可能还有许多其他订单).但是可以编辑订单实体的平面字段以及订单的AddressID"以分配另一个地址.

... is NOT possible. So it is not possible in the GridView to edit the City (which makes sense because its a related field belonging to another table and perhaps to many other orders). But it is possible to edit flat fields of the order entity and also the "AddressID" of the order to assign another address.

这篇关于带有 EntityDataSource 的一个 ASP.NET GridView 中两个相关数据库表的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 01:13