This is a migrated thread and some comments may be shown as answers.

Join Tables

2 Answers 89 Views
Getting Started
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Mike
Top achievements
Rank 1
Mike asked on 10 Apr 2013, 08:07 AM
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 Answers, 1 is accepted

Sort by
0
Mike
Top achievements
Rank 1
answered on 11 Apr 2013, 08:14 AM
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?
0
Doroteya
Telerik team
answered on 12 Apr 2013, 07:33 AM
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 >>
Tags
Getting Started
Asked by
Mike
Top achievements
Rank 1
Answers by
Mike
Top achievements
Rank 1
Doroteya
Telerik team
Share this question
or