简体   繁体   中英

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 (eg 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

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."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM