访问实体数据框架中仅包含外键的表

时间:2011-07-18 13:32:27

标签: entity-framework

我是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创建的类,因为它没有任何其他列名,ActionIdRoleID

先谢谢你

2 个答案:

答案 0 :(得分:1)

当你有这样的链接表时,将所有表添加到实体模型应该在2个结束表之间创建双向关系属性,完全隐藏链接表,允许您通过以下方式访问:

IEnumerable<string> roleNames = Entities.Actions
    .First(a => a.Actionid == actionid)
    .Roles
    .Select(r => r.Rolename);

其中actionidint 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

相关问题