Join Tables

3 posts, 0 answers
  1. Mike
    Mike avatar
    31 posts
    Member since:
    Apr 2009

    Posted 10 Apr 2013 Link to this post

    I have defined three database tables:
    - User (UserId, Username),
    - Role (RoleId, RoleName)
    - UserRoles (UserId, RoleId)

    When Telerik ORM imports the tables into a model it does not create an entity for UserRoles. I believe this is because it is deemed to be a join table?
    As a result of this I'm given to understand that the only way to create a UserRole would be to either add a User to the Role.Users list or add a Role to the User.Roles list.
    In theory this sounds fine but I have come concerns. It would the ORM layer will execute 4 seperate sql statements when I want to create a single record. Here is my code and reasoning:
    'Define the context
    Dim portal As New DbContext
    'Get the user entity
    Dim user = (From u In portal.Users Where u.UserId = UserId Select u).FirstOrDefault 'SQL CALL #1
    'Get the role entity
    Dim role = (From r In portal.Roles Where r.RoleId = RoleId Select r).FirstOrDefault 'SQL CALL #2
    'Add the role to the user (insert the record)
    user.Roles.Add(role) 'SQL CALL #3
    'Save the changes
    portal.Add(user)   
    portal.SaveChanges() 'SQL CALL #4

    Is there a way to simply create a UserRole record without having to fetch the user, role, user roles and then finally insert the record?
  2. Mike
    Mike avatar
    31 posts
    Member since:
    Apr 2009

    Posted 11 Apr 2013 Link to this post

    Help!
    Is there any way to tell the database wizard to import Join Tables as entities??
    In the above example this would mean my object model would contain User, Role and UserRole entities instead of just the User and Role ones? This would help me greatly?
  3. DevCraft banner
  4. Doroteya
    Admin
    Doroteya avatar
    502 posts

    Posted 12 Apr 2013 Link to this post

    Hi Mike,

    In order to achieve the necessary result, you would need to modify the domain model in Visual Designer as follows:
    1) Open the .rlinq file in Visual Designer
    2) Delete the many-to-many association between the User and Role entities
    3) Open Model Schema Explorer and expand the Tables node
    4) Find the join table and drag-and-drop it on the designer's surface.
    5) Save the .rlinq file

    I hope that helps. If you have other questions, do not hesitate to get back to us.


    Regards,
    Doroteya
    the Telerik team
    Using Encrypted Connection Strings with Telerik OpenAccess ORM. Read our latest blog article >>
Back to Top