This question is locked. New answers and comments are not allowed.
Hi,
we are building a simple import / export function for our database.
We first delete all objects in the table and then import the objects from the import file.
We wanted to handle these two actions (delete / add) in one transaction so that we don't get a corrupted db if the import fails and all the database entries are already deleted.
But if we delete the db entries and then import the objects from the import file and a object from the import are the same as in the database we get an error 'Instance of 'class' with identity 'guid' already exists in the cache of the object scope.'
From this thread (http://www.telerik.com/forums/instance-of-class-with-identity-guid-already-exists-in-the-cache-of-the-object-scope#763380) I found that it only is remarked as remove and still available in the scope.
We also tried to use dbContext.FlushChanges() but this didn't solve the problem.
1. Is there already a possibility to do the two things in one transaction?
2. If not what would be the best alternative to handle this? Backup the items from the database, delete the items, add the items, and if anything fails, reimport the old items?
Here is the code we use:
Thank you
Best regards,
Mark
we are building a simple import / export function for our database.
We first delete all objects in the table and then import the objects from the import file.
We wanted to handle these two actions (delete / add) in one transaction so that we don't get a corrupted db if the import fails and all the database entries are already deleted.
But if we delete the db entries and then import the objects from the import file and a object from the import are the same as in the database we get an error 'Instance of 'class' with identity 'guid' already exists in the cache of the object scope.'
From this thread (http://www.telerik.com/forums/instance-of-class-with-identity-guid-already-exists-in-the-cache-of-the-object-scope#763380) I found that it only is remarked as remove and still available in the scope.
We also tried to use dbContext.FlushChanges() but this didn't solve the problem.
1. Is there already a possibility to do the two things in one transaction?
2. If not what would be the best alternative to handle this? Backup the items from the database, delete the items, add the items, and if anything fails, reimport the old items?
Here is the code we use:
try
{
var preferredPhoneNumbers = _dbContext.PreferredPhoneNumbers;
foreach
(var preferredPhoneNumber
in
preferredPhoneNumbers)
{
_dbContext.Delete(preferredPhoneNumber);
(_dbContext
as
OpenAccessContext).FlushChanges();
}
foreach
(var phoneNumber
in
phoneNumbersList)
{
_dbContext.Add(phoneNumber);
}
_dbContext.SaveChanges();
}
catch
(Exception e)
{
NLogger.LogError(
"Error: "
+ e);
_dbContext.ClearChanges();
}
Thank you
Best regards,
Mark