none
DataContext throws duplicate exception for EVERY record after first dup detected RRS feed

  • Question

  • I am puzzeled as to why after throwing a legitimate duplcate record exception, I get the same error thrown for EVERY record I attempt to insert after the exception, even if they aren't dups.

    I found a work around was to create a new DataContext instance prior to everu InsertOnSubmit/SubmitChanges pairing. However this does not seem efficient.

    Is there some way to restore DataContext to a non-errored state without creating a new instance?

    -----------------------------------------------------------------------------------------------------------------------------------

    Examples:

    I) InsertStuff FAILS for EVERY call after first dup detected:

    class MyClass
    {
       static MyDatacontext ctx = null;

       MyClass
       {
           ctx = new MyDataContext("mydbconnection");
        }

        poblic static void InsertStuff(t_customers cust)
        { 
             ctx.t_customers.InsertOnSubmit(cust):
             ctx.SubmitChanges();
         }
    }

    II) This works OK for non-dup inserts, but doesn't seem efficient:

    class MyClass
    {
         poblic static void InsertStuff(t_customers cust)
        {  
             MyDataContext ctx = new MyDataContext("mydbconnection");
             ctx.t_customers.InsertOnSubmit(cust):
             ctx.SubmitChanges();
         }
    }

    • Edited by Bill2010 Sunday, January 25, 2009 9:13 PM
    Sunday, January 25, 2009 4:53 PM

Answers

All replies

  • Hi,

     

    Is it possible that the first Entity that caused the exception is still attached to the DataContext, such that everytime you try to SubmitChanges(), it causes the same exception again and again ? or do you dettach it from the DataContext after the first exception ?

    Regards.


    Luc Morin, T.P. http://www.stlm.ca
    Sunday, January 25, 2009 5:19 PM
  • I thought about that, but couldn't find any way to delete the bad records if they are still there.
    Sunday, January 25, 2009 9:15 PM
  • You can call Table.DeleteOnSubmit(x) to undo a bad/duplicate Table.InsertOnSubmit(x) call.
    Wayward LINQ Lacky
    Tuesday, January 27, 2009 1:03 AM
    Moderator