Exception duplicated key

4 posts, 1 answers
  1. Steve
    Steve avatar
    50 posts
    Member since:
    Apr 2013

    Posted 05 Jun 2013 Link to this post

    Hi,

    I'm trying RadGridView and OpenAccess after SaveChange exception duplicated key error, Please see screenshot.
  2. Doroteya
    Admin
    Doroteya avatar
    498 posts

    Posted 10 Jun 2013 Link to this post

    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.
  3. Steve
    Steve avatar
    50 posts
    Member since:
    Apr 2013

    Posted 10 Jun 2013 Link to this post

    Hi Doroteya,

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

    Thanks.
  4. Answer
    Doroteya
    Admin
    Doroteya avatar
    498 posts

    Posted 14 Jun 2013 Link to this post

    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.
Back to Top