Riccardo Francescato
Riccardo Francescato

Reputation: 23

c# linq discrad InsertOnSubmit

i have a problem while using SubmitChanges() and InsertOnSubmit(), if the SubmitChanges fail and raise an exception the change set remains filled with the object that have a conflict in the db (e.g. the primary key already exists). the problem is that if i re-execute the piece of code that insert the element in the database the SubmitChanges fail because the object with the wrong primary key is in the change set. The question is how to delete the first obj from the change set?

private void bInsInserisci_Click(object sender, RoutedEventArgs e)
    {
        Prodotto tmp = new Prodotto();
        try {
            tmp.idProdotto = InsProdCodProd.Text;
            tmp.tempo = Int32.Parse(InsProdTemp.Text);
            tmp.note = InsProdNote.Text;
            prodotti.InsertOnSubmit(tmp);
            db.SubmitChanges();
        }
        catch {
            System.Data.Linq.RefreshMode.KeepCurrentValues
        }

    }

Thanks

Upvotes: 0

Views: 227

Answers (1)

scottjustin5000
scottjustin5000

Reputation: 1346

Why not discard the context? Take a look at this article concerning the lifetime of a L2S datacontext. " DataContext could be used after SubmitChanges() but one has to be careful."

Upvotes: 0

Related Questions