开发者

Accessing table with only foreign Keys in Entity Data Framework

开发者 https://www.devze.com 2023-03-21 15:38 出处:网络
i am 开发者_如何学运维new to Entity Data framework i have tables Actions ( Actionid (pk) ,Actionname , ... )

i am 开发者_如何学运维new to Entity Data framework i have tables

Actions ( Actionid (pk) ,Actionname , ... )
Roles ( Roleid(pk) , Rolename , .... ) 
ActionRoles( Actionid(pk,fk)  , Roleid(fk) ) [Mapping Table]

Please Suggest me the LINQ to get the RoleNames for Perticular ActionID (Note : there is No class created with Name ActionRoles in entitydesigner.cs as because it doesn't have any other column name then ActionId and RoleID )

Thank you in Advance


When you have a link table like this, adding all tables to the Entity Model should create 2 way relationship Properties between the 2 end tables, hiding the link table completely allowing you to access via something like:

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

where actionid is an int variable containing the actionid you're interested in.


For a discussion of how to handle many-to-many relationships such as this (both foreign keys must be in the ActionRoles primary key as indicated in the comment to your question), see these tutorials: For EF 4.0: http://www.asp.net/entity-framework/tutorials/the-entity-framework-and-aspnet-–-getting-started-part-5 For Ef 4.1: http://www.asp.net/entity-framework/tutorials/updating-related-data-with-the-entity-framework-in-an-asp-net-mvc-application

0

精彩评论

暂无评论...
验证码 换一张
取 消