I have to following problem.
I’ve large collection of XML files. In each XML file there are different employees. I have to add the unique employees to the database. In the database each employee has first name, last name and birth date. I want to check whether employee with the same properties already exists in the database and if it doesn’t to insert it.
I’m using the following code:
Entities entities;
entities.AddToEmployee(emp);
entities.SaveChanges();
My employee is connected with another data structure – sales. So I need to add just the employee’s primary key to the sales table but not the whole employee in the database (in case that it already exists).
Thanks in advance!
The ‘Find or Add Pattern’ can be useful in these types of circumstance:
This way the emp object has been loaded if it exists in the database or created and saved to the database. You might need to replace the Find() with a Where() if you are searching on multiple properties.