Question

I have a Customer table that I'm inserting records into via Entity Framework. The table has an integer primary key (custid). Before adding the Customer record to the context, I'm querying the database to get the last key used, then setting custid to one higher than that.

However, it's possible that in between the query and saving the new record to the database, another client will store a record with that custid, so when I call SaveChanges(), I will get an exception due to what is now a duplicate value.

My plan is to put the SaveChanges() call in a try{} block, catch the exception, and then increment the saleid by one more, and try again.

What I'm not sure about is how to change the value of custid, now that it's already been added to the context. My code is something like this:

CUSTOMER myCustomer = new CUSTOMER();
myCustomer.FirstName = "John";
myCustomer.LastName = "Smith";
myCustomer.Custid = GetLastCustid() + 1;

myContext.CUSTOMERS.Add(myCustomer);

bool SavedOK = false;
while (!SavedOK)
{
    try 
    {
        myContext.SaveChanges();
        SavedOK = true;
    }
    catch (DBUpdateException e)
    {
        if (IsCustidConflict(e))
        {
            // Increment Custid by one 
            // How do I do that?????
            continue;
        }
    }
}
Was it helpful?

Solution

The DbUpdateException class contains an Entries property from which you can extract the collection of the entries failed to persist:

catch (DBUpdateException e)
    {
        if (IsCustidConflict(e))
        {
            foreach (var entry in e.Entries)
            {
                  if (entry.Entity is CUSTOMER)
                  {
                        var customer = entry.Entity as CUSTOMER;
                        customer.Custid++;
                  }
            }

            continue;
        }
    }

As a side note, I guess you have a good reason why you aren't letting the database generate the primary-key for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top