This is a migrated thread and some comments may be shown as answers.

Exception duplicated key

3 Answers 58 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Steve
Top achievements
Rank 1
Steve asked on 05 Jun 2013, 05:07 PM
Hi,

I'm trying RadGridView and OpenAccess after SaveChange exception duplicated key error, Please see screenshot.

3 Answers, 1 is accepted

Sort by
0
Doroteya
Telerik team
answered on 10 Jun 2013, 11:51 AM
Hi Steve,

Generally, the error you experience is thrown when the value of the identity property of the inserted object is the same as the value of the identity column of an object that is already present in the database table. The idea behind the primary key is: this is a table column (or a combination of table columns) that contains a value which uniquely identifies every record in the table.
 
In your case, it seems that the ID property of the new Customer object you attempt to insert has value equal to 0 and that the Customer table in the database already contains a record with the same value for its primary key column.

I would suggest to you to change the value for the identity property of the new object and try to persist it again.

If you continue to experience the error after that, could you please send us a sample code that demonstrates it?

I hope you find that feasible. I am looking forward to your feedback.


Regards,
Doroteya
Telerik
OpenAccess Samples Kit boasts 50+ sample applications providing diverse real-life business solutions. Click to read more and see OpenAccess ORM in action.
0
Steve
Top achievements
Rank 1
answered on 10 Jun 2013, 01:12 PM
Hi Doroteya,

How to Automatic change primary key and how to custom change primary key value please take a example.

Thanks.
0
Accepted
Doroteya
Telerik team
answered on 14 Jun 2013, 10:43 AM
Hi Steve,

Generally, the way the primary keys are handled strongly depends on the implemented scenario. Basically, Telerik OpenAccess ORM generates the SQL statements according to both the set up of the database tables and the setup of the domain classes. Two very basic examples for automatic and manual changes would be:

1) Automatic primary key generation - the primary key column of a database table is of type INT and is set as IDENTITY. In this case the model should be set up as described here. The result would be that the column will not be included in the INSERT statements and you will not have to take care for setting an explicit value for the corresponding property in your code.

2) Manual primary key generation - The primary key column is of type INT but is not set as IDENTITY. In this case OpenAccess ORM will include the column in the generated INSERT statements and you will have to assign a value for the primary key column of each new row like this:
Product newProduct = new Product()
{
    ProductID = 1,
    ProductName = "Product Name"
};
Where ProductID is would be the primary key for the Products table.

The workflow for inserting objects in the database is described here.

If your scenario differs from the described ones, or if you experience difficulties with the implementation, do not hesitate to get back to us.  


Regards,
Doroteya
Telerik
OpenAccess ORM Q2 2013 brings you a more powerful code generation and a unique Bulk Operations support with LINQ syntax. Check out the list of new functionality and improvements shipped with this release.
Tags
GridView
Asked by
Steve
Top achievements
Rank 1
Answers by
Doroteya
Telerik team
Steve
Top achievements
Rank 1
Share this question
or