DuplicateKey Error
Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce Classic
Forum Discription: For .NET 2.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=625
Printed Date: 11-Jun-2026 at 9:18am
Topic: DuplicateKey Error
Posted By: aladdin
Subject: DuplicateKey Error
Date Posted: 18-Dec-2007 at 2:15pm
When create new entity(on added before save) the PersistenceManager use the PooledNumericIdGenerator class, to create primary key to non AutoIncrement field and post it -1.Now, after save if created duplicated key how can the Verification engine to catch the error unfortunately used AdoHelper methods.
Thanks for all.
|
Replies:
Posted By: pkarsh
Date Posted: 19-Dec-2007 at 4:20pm
If you were to use the Verification engine to try to catch this error you would have to write your own custom verifier. In this case that would be quite complicated. That is probably not the right way to accomplish what you want to do.
If you are using the PooledNumericIdGenerator then in your NextId table you have to have an entry for the name of the table that you are doing Id generation for. If you have that then you should not get a duplicate key error.
Hope this helps.
Paul Karsh
IdeaBlade Support
|
Posted By: aladdin
Date Posted: 29-Jan-2008 at 9:29am
Dear Paul,
about your second opinion, if deleted the last record of Invoices then NextId table loose the serial number, and
the user must be have permission to modify the NextId table. Now, if the user is stupid maybe save wrong id in
the NextId table. In this case we get a duplicate key error.How can to protect ?
Thanks
|
Posted By: pkarsh
Date Posted: 29-Jan-2008 at 10:39am
I'm afraid I don't understand your question. How would deleting the last record of a table cause you to lose the value entered in the NextId table? Are you trying to re-use primary keys of deleted records? We recommend against that. Under ordinary circumstances the Id generator and not end-users should be writing into the NextId table. If for some reason you cannot adhere to this rule, then I suppose you could put your SaveChanges call into a try-catch loop (which is a good idea anyway) and do something to handle a Save failure due to a Duplicate key error. If your primary keys are in ascending order, meaning, for example, that you would not assign a primary key of 3 after having assigned one of 4, then you might consider running a query to get the MAX value of the primary key column.
If you wish to send me a private email to explain your situation in more detail then click on the link to my user name and you will see a button that will enable you to do so.
Paul Karsh
IdeaBlade Support
|
|