LinqToSQL Double Insert issue

Posted by Vaccano on Stack Overflow See other posts from Stack Overflow or by Vaccano
Published on 2010-03-17T18:09:22Z Indexed on 2010/03/17 18:11 UTC
Read the original article Hit count: 435

Filed under:
|
|
|

I have a WCF service with an object structure similar to this:

public class MyClass
{
    public List<MySubItem> SubItems { get; set; }
}

public class MySubItem
{
    public List<MySubSubItem> SubSubItems { get; set; }
}

public class MySubSubItem
{
    public string DataValue { get; set; }
}

public class MyClassDAL
{
    public void InsertMyClass(MyClass myClass)
    {
        ctx.MyClasses.InsertOnSubmit(myClass);
        ctx.SubmitChanges();
    }
}

Sometimes my client will call in with a MyClass that submits only half of the values that it has in the list SubSubItems.

Later it calls the insert with the rest of the list. The problem is that when it does this I get a Primary Key violation. The reason is that it is trying to insert the MySubItem again (because there are more items in the SubSubItems owned by the same MySubItem object).

How do I deal with this? Do I just call an Update? Do I have to try to separate them out (updates from inserts)?

SQL Server 2008 has a really cool Merge functionally. Is there some way to access that from LinqToSQL?

© Stack Overflow or respective owner

Related posts about LinqToSQL

Related posts about sql-server-2008