Linq to SQL EntitySet Records causing duplicate insertion

Posted by Savvas Sopiadis on Stack Overflow See other posts from Stack Overflow or by Savvas Sopiadis
Published on 2009-12-18T10:11:53Z Indexed on 2010/06/17 19:03 UTC
Read the original article Hit count: 259

Filed under:

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

© Stack Overflow or respective owner

Related posts about linq-to-sql