在 ASP.NET GridView 控件中显示实体模型中的多对多关系
假设我有两个表:Cars 和 BodyStyle 具有多对多关系。
在数据实体模型中,它会显示两个表,但在 Sql 中,它有一个辅助表可以链接,因为它是多对多关系。
我想知道如何在 gridview 中显示与 Car 实例相关的所有样式。
所以基本上 gridview 应该看起来像:
ID |制作|型号|风格|
----------------------------------'
1 |讴歌 | EL |豪华轿车|
2 |讴歌 | MDX |豪华SUV|
我似乎无法通过 c# 访问帮助表
抱歉,我是 c# 编程新手。
Lets say i have two tables: Cars and BodyStyle with a many to many relationship.
In Data Entity Model it would show the two tables but in Sql it has a helper table to link since its a many to many relationship.
I want to know how is it possible to display the all the Styles related to an instance of Car inside a gridview.
so basically the gridview should look like:
ID | Make | Model | Style |
----------------------------------'
1 | Acura | EL | Sedan, Luxury|
2 | Acura | MDX | SUV, Luxury|
I cant seem to access the helping table through c#
Sorry I am new to c# programming.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可以不将辅助表添加到 edm 中吗?我将从添加它开始,否则我如何知道辅助表的存在。
Can you not add the helper table into the edm? I would start with adding that or else how would the m know of the existence of the helper table.
你有几个选择可以做到这一点。您的 Car 类应具有 Models Navigation 属性,并且您的 Model 类应具有 Cars 导航属性。
第一个选项:编写一个存储过程,该过程将接受特定(汽车/模型)作为参数并相应地返回结果集,然后将该 sp 映射到 edm 中。然后,您可以从代码隐藏中调用 SP(如果您没有使用 Reporsitoy/Separation of Concern)并绑定到 gridview。
第二个选项:如果您有 POCO 汽车类,请编写一个名为 Models 的导航属性,该属性基本上会查询您的数据库(使用 LINQ to SQL)并获取所有模型,然后您只需在 UI 中调用该属性(如 IEnumerable)并绑定到gridview。
希望这有帮助,并希望我能正确理解你的问题
You got couple of options to do that. Your Car class should have Models Navigation property and your Model class should have Cars navigation property.
First option: Write a stored procedure which will accept the particular (car/model) as a parameter and return back the resultset accordingly and then map that sp in the edm. You can then call the SP from you code-behind (if you are not using Reporsitoy/Separation of Concern) and bind to the gridview.
Second option: If you are having POCO car class, write a Navigation property named Models which will basically query your db (using LINQ to SQL) and get all the models and then You just need to call that property (as IEnumerable) in your UI and bind to gridview.
Hope this helps and hope I have understood your question appropriately