Duplicate Key Entry

Discussion of open issues, suggestions and bugs regarding LinqConnect – Devart's LINQ to SQL compatible ORM
Post Reply
Zero-G.
Posts: 398
Joined: Sat 09 Dec 2006 12:20

Duplicate Key Entry

Post by Zero-G. » Wed 15 Dec 2010 10:29

Hey

I use your latest components of v6 mySQL dotConnect.
On the server, I have set a unique key, which goes over 5 fields.

When the user enters a duplicate article, then the error is thrown, as expectet.
But my question is: how can I remove this article from the Datacontext, so this input will not be tried again!?

THX

StanislavK
Devart Team
Posts: 1710
Joined: Thu 03 Dec 2009 10:48

Post by StanislavK » Thu 16 Dec 2010 16:36

As I can understand, you need to cancel changes made in DataContext so that the next call of SubmitChanges won't fail as well. Am I correct? For this purpose, please use the RejectChanges method:

Code: Select all

Dept newDept = new Dept() { Deptno = 10, Dname = "New Dept", Loc = "Somewhere" };
dataContext.Depts.InsertOnSubmit(newDept);
      
try {
	dataContext.SubmitChanges();
}
catch(Exception ex)
{        
	dataContext.RejectChanges();
}
Here we try to insert a new entity, and if SubmitChanges fails, we call RejectChanges to remove the problem entity from DataContext.

Please tell us if this helps.

Post Reply