我是Entity Data框架的新手 我有桌子
Actions ( Actionid (pk) ,Actionname , ... )
Roles ( Roleid(pk) , Rolename , .... )
ActionRoles( Actionid(pk,fk) , Roleid(fk) ) [Mapping Table]
请建议我使用LINQ获取RoleNames
for Perticular ActionID
(注意:在entitydesigner.cs中没有使用名称ActionRoles
创建的类,因为它没有任何其他列名,ActionId
和RoleID
)
先谢谢你
答案 0 :(得分:1)
当你有这样的链接表时,将所有表添加到实体模型应该在2个结束表之间创建双向关系属性,完全隐藏链接表,允许您通过以下方式访问:
IEnumerable<string> roleNames = Entities.Actions
.First(a => a.Actionid == actionid)
.Roles
.Select(r => r.Rolename);
其中actionid
是int variable
,其中包含您感兴趣的actionid
。
答案 1 :(得分:1)
有关如何处理多对多关系的讨论(两个外键必须在ActionRoles主键中,如问题的注释中所示),请参阅以下教程: 对于EF 4.0:http://www.asp.net/entity-framework/tutorials/the-entity-framework-and-aspnet- - getting-started-part-5 对于Ef 4.1:http://www.asp.net/entity-framework/tutorials/updating-related-data-with-the-entity-framework-in-an-asp-net-mvc-application