How we get data Many to many Relationship in EDM

Last post 06-03-2009 10:41 AM by Qasimali84. 2 replies.

Sort Posts:

  • How we get data Many to many Relationship in EDM

    05-09-2009, 2:36 AM
    • Member
      29 point Member
    • Qasimali84
    • Member since 04-10-2008, 7:49 AM
    • Faisalabad
    • Posts 47
    I have three entities having many to many relationship in my EntityDesigner Diagram.
    1. User
    2.Group
    3. Role
     
    1. User Entity has following structure
      
     Scalar Properties
        ID
        UserName
        Password
       Active
       Email
    Navigation Properties
      Groups
     
     2.Group Entities has following structure 
      
    Scalar Properties
        ID
        Name
     Navigation Properties
      Users
      Roles
     
    3.Role Entities has following structure 
      
    Scalar Properties
        ID
        Name
     Navigation Properties
     Groups
     

    Now I have to
    1. Show All the roles against a group id in a gride view
    2. All the roles assigned against a userid in an other gride view

    How this task will be accomplished using EDM.
    What queries I have to write and what namespaces I have to use

    Thansk in Advance.
    Entity Design Model
  • Re: How we get data Many to many Relationship in EDM

    05-20-2009, 3:12 AM
    Answer
    • Contributor
      2,074 point Contributor
    • cgeers
    • Member since 02-21-2009, 7:41 AM
    • Belgium
    • Posts 275

    Qasimali84,

    Based on your question I assume the following:

    • You have 5 tables in your database, namely [User], [Group], [Role], [GroupRoles], [GroupUsers]
    • The table [GroupRoles] links roles to groups and only contains two fields which are the primary keys of the tables [Group] and [Role]
    • The table [GroupUsers] links users to groups and only contains two fields which are the primary keys of the tables [Group] and [User]
    • The tables [GroupRoles] and [GroupUsers] don't show up as separate entities in the model but are directly represented as navigation properties

    Then the code to perform your two queries (Linq to Entities) is the following:
    1    using (Entities context = new Entities())
    2 {
    3 // Display the roles of a certain group
    4 var q = (from g in context.Groups
    5 where g.Id == 1
    6 select g.Roles).FirstOrDefault();
    7 foreach(var role in q)
    8 {
    9 Console.WriteLine(role.RoleName);
    10 }
    11
    12 Console.WriteLine();
    13 // Display the roles of a certain user
    14 var q2 = (from g in context.Groups
    15 where g.Users.Any(u => u.Id == 1)
    16 select g.Roles).FirstOrDefault();
    17 foreach(var role in q2)
    18 {
    19 Console.WriteLine(role.RoleName);
    20 }
    21 }
    If you want the source code, just send me a private message with your e-mail address.

    Regards,

    Christophe

    Please mark my reply as an answer if it helps you.

    Check out my blog
  • Re: How we get data Many to many Relationship in EDM

    06-03-2009, 10:41 AM
    • Member
      29 point Member
    • Qasimali84
    • Member since 04-10-2008, 7:49 AM
    • Faisalabad
    • Posts 47

    Great hint It solved my problem

Page 1 of 1 (3 items)