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

Many to many mapping and insert, update operation

3 Answers 96 Views
Data Access Free Edition
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Peenty
Top achievements
Rank 1
Peenty asked on 15 Feb 2012, 01:19 PM

Hello,
I have many to many mapping on basis tables:

Roles
Id int, PK
Name varchar(50), not null

Privileges
Id int, PK
Name varchar(50), not null

RolePrivileges
RoleId int, PK, FK to Roles.Id
PrivilegeId int, PK, FK to Privileges.Id

I have existing privileges (in table Privileges) and I'd like to insert a new Role with selected privileges.
I try someting like this:

Role role = new Role();
role.Name = "Role ABC";
role.Privileges.Add(new Privilege { Id = 1, Name = "Privilege 1" });
role.Privileges.Add(new Privilege { Id = 3, Name = "Privilege 3" });
dbContext.AddRole(role);
After this I get error Telerik.OpenAccess.Exceptions.DuplicateKeyException because it tries to insert
new privileges (to Privileges table) instead of to insert new row to RolePrivileges table.
Regards
k

3 Answers, 1 is accepted

Sort by
0
Damyan Bogoev
Telerik team
answered on 16 Feb 2012, 04:40 PM

Hello Krzysztof,

You should slightly modify this code snippet in the following way:

Role role = new Role();
role.Name = "Role ABC";
Privilege firstPrivilege = dbContext.Privileges.FirstOrDefault(x => x.Id == 1);
Privilege secondPrivilege = dbContext.Privileges.FirstOrDefault(x => x.Id == 3);
role.Privileges.Add(firstPrivilege);
role.Privileges.Add(secondPrivilege);
  
dbContext.AddRole(role);

Hope that helps.


Regards,
Damyan Bogoev
the Telerik team
Want to use Telerik OpenAccess with SQL Azure? Download the trial version today. 
0
KFS
Top achievements
Rank 1
answered on 18 Jul 2016, 03:02 PM

How can that managed if the select and insert operation managed by different DB context ?

Privilege firstPrivilege =  dbContext1.CreateDetachedCopy( dbContext1.Privileges.FirstOrDefault(x => x.Id ==  1));
 
Role role = new Role();
 
role.Name = "Role ABC";
role.Privileges.Add(firstPrivilege);
 
dbContext2.AddRole(role);

This cause create new privilege and insert the new privilege Id in table RolePrivileges.

Thanks,

0
Boris Georgiev
Telerik team
answered on 21 Jul 2016, 06:34 PM
Hi,

I was not able to reproduce this behavior. The observed and default behavior in this scenario is that only new role will be created in the database and one record will be added in the Join table between the id of the new role and the id of the detached privilege. To achieve this behavior you should add the new created role to the navigation property of the detached privilege and Attach the role to the context, for that you should use AttachCopy method instead Add method.

For your convenience I attached a sample which demonstrates the workflow.

I hope that helps.

Regards,
Boris Georgiev
Telerik by Progress
 
Check out the latest announcement about Telerik Data Access vNext as a powerful framework able to solve core development problems.
Tags
Data Access Free Edition
Asked by
Peenty
Top achievements
Rank 1
Answers by
Damyan Bogoev
Telerik team
KFS
Top achievements
Rank 1
Boris Georgiev
Telerik team
Share this question
or