Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a table which stores Employee Id, Names, Skills, Salary of an employee. I have one more table which has Skill Name and Skill Id and one more mapping table which has two foreign key Employee Id and Skill Id which automatically gets updated when some selects a skill. Now I have created an Update Method in controller which take previous record of the particular employee if the user has updated means selected another new skill the mapping table is adding the records of the all the skill again which is causing an error of primary key constraint because previous skills are already mapped i just want to map the new skill in the mapping table. Thank you


What I have tried:

I have tried fetching both the values previous as well as new+previous and adding the new value to an array and passing it to db context update method but the previous skills are also going in for mapping which i dont want
Posted
Updated 22-Feb-23 23:37pm
Comments
PIEBALDconsult 22-Feb-23 23:36pm    
Which database system? If SQL Server, maybe try a MERGE statement.
Shriyansh Dubey 23-Feb-23 0:03am    
Yes sql server but i am using code first approach is possible to add condition to the context.savechanges to save only those mappings which is unique
PIEBALDconsult 23-Feb-23 8:01am    
I wouldn't know, I always do things the right way.
Shriyansh Dubey 23-Feb-23 0:04am    
Sorry I just started learning these concepts so maybe my explanation won't be much clear.
Member 15627495 23-Feb-23 0:05am    
add a primary key (by AUTO-INCREMENT) to your Mapping table ( instead of overwriting old value ).

it will be fill with a 'insert into Mapping_table(emp_id,skill_id) value (emp_id_value, skill_id_value);' instead of 'update' query.

then :
- add a column to 'Mapping table' [ as primary key auto increment ]
- use 'insert' query

1 solution

This is a standard update problem for any use case and there are multiple ways to solve it.
Lets take your example.

SQL
create table Employee 
(
   EmployeeId int primary key,
   Names varchar(100),
   skills varchar(200),
   Salary numeric(10,2)
)

create table Skill 
(
   Skillid int primary key,
   Names varchar(100)
 )

CREATE TABLE EmployeeSkillMapping
( employeeid INT FOREIGN KEY REFERENCES Employee(EmployeeId),
  skillid INT FOREIGN KEY REFERENCES skill(Skillid),
)

Now let's add some dummy data

SQL
INSERT INTO Employee(employeeid, names, salary)
SELECT 1, 'Test1', 100

insert into skill (skillid, names)
SELECT 1, 'C++'
UNION ALL
SELECT 2, 'C#'
UNION ALL
SELECT 3, 'Java'
UNION ALL
SELECT 4, 'ReactJS'
UNION ALL
SELECT 5, 'NodeJS'

insert into EmployeeSkillMapping(employeeid, skillid)
SELECT 1, 1
UNION ALL
SELECT 1, 2
UNION ALL
SELECT 1, 3


The usecase here is Employee Name "Test" have three skills (C++, C# and Java). Now we need to modify Asif by removing Java skill and add new ReactJS skill.

Usually the interface for doing above modifications is on the left side we have all available skills and at the right side we have assigned skills list.

One elegant approach is to use MERGE. Merge command can Insert/Update/Delete in one transaction. You can call an SP/SQL from code passing all assigned skills list to the SP and the SP will do the Job, may be like this.

SQL
MERGE EmployeeSkillMapping AS tgt  
USING (
  VALUES 
        (1, 1),
		(1, 2),
        (1, 4)
) AS src (employeeid, skillid) 
    ON tgt.employeeid = src.employeeid and tgt.skillid = src.skillid
WHEN NOT MATCHED THEN  
    INSERT (employeeid, skillid)  
    VALUES (src.employeeid, src.skillid) 
WHEN NOT MATCHED BY SOURCE THEN
  DELETE


Another approach is to break it into two parts. One is for Insert and another is for deletion. You already have the previous assigned list while you fetched employee details. Insert case is simple where you know the newly assigned skills are not present by comparing new values with session values. For the delete case, you need to know which skills have been removed by again comparing new values with session values.
Once you have both data sets you can separately execute Insert and delete statements.

This should give you a head start. Good Luck!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900