Linq to SQL EntitySet Records causing duplicate insertion
- by Savvas Sopiadis
In a WPF application i'm using Linq to SQL in a multi tier application.
(This is an archailogy photo filing application), so every excavation has its corresponding Pictures, thus a one-to-many relationship. This relationship is correctly created by SQLMetal (which i'm using to create the POCOs).
So here is the situation i 'm having trouble with:
Saving changes (either of new or altered objects) is done through UnitOfWork() pattern this way:
using (IUnitOfWork unitOfWork = UnitOfWork.Begin())
{
//if this is a new record
if (SelectedExcavation.excavation.ExcavationId == 0)
{
IsNewRecord = true;
excavService.Add(SelectedExcavation.excavation);
}
//send the actual changes to the dbms
unitOfWork.Commit();
}
Everything works fine!
BUTTTT!!!
Whenever a record gets updated which has (already at least one ) corresponding Picture Record:
1) a new Excavation Record is inserted
2) the current Excavation Record gets updated
3) the previous Picture Record gets its Id changed to the newly created ExcavationId
What is going on under the hood?
Does Linq to SQL not handle such simple update scenarios?
Thanks in advance