Search code examples
c#winformslinqdevexpressxpo

How to create dupplicate Key after old key deleted on Devexpress XPO?


This is my product Class:

  public class Product : XPLiteObject
    {
       public string productId {get;set;}
    }

i use Gridview to control Data. When i delete a row with productId = "id-5";

gridview1.deleteSelectedRows();

then Add new row with productId = "id-5"; (same id i was deleted)

Finally i comit;

unitOfWork1.CommitChanges();

And exception recieved: A dupplicate key found !


Solution

  • It looks like a corresponding record was not removed from the database. There are three possible reasons:

    1. If the GridView is bound to the XPCollection, make sure that the XPCollection.DeleteObjectOnRemove property is set to true. Otherwise, the GridView.DeleteSelectedRows method deletes object from the collection only;

    2. If the GridView bound to the XPCollection through the UnitOfWork, you also need to execute the UnitOfWork.CommitChanges method after executing the GridView.DeleteSelectedRows. Otherwise, your changes will not be committed to the database;

    3. If the Product class is decorated with the DeferredDeletionAttribute, deleting this object will set a corresponding boolean column value, but will not actually delete the record from the database. If this is the case, there are four ways to resolve the problem: