简体   繁体   English

时间:2019-01-16标签:c#linqdisradInsertOnSubmit

[英]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). 我在使用SubmitChanges()和InsertOnSubmit()时遇到问题,如果SubmitChanges失败并引发异常,则更改集仍将填充数据库中有冲突的对象(例如主键已存在)。 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. 问题是,如果我重新执行在数据库中插入元素的代码,则SubmitChanges将失败,因为具有错误主键的对象位于更改集中。 The question is how to delete the first obj from the change set? 问题是如何从更改集中删除第一个obj?

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. 看看这个关于一个L2S的datacontext的寿命文章。 " DataContext could be used after SubmitChanges() but one has to be careful." “ DataContext可以在SubmitChanges()之后使用,但必须小心。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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