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

Import / Export Database

2 Answers 60 Views
Development (API, general questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Mark
Top achievements
Rank 1
Mark asked on 25 Sep 2014, 01:21 PM
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:
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


2 Answers, 1 is accepted

Sort by
0
Doroteya
Telerik team
answered on 29 Sep 2014, 06:33 AM
Hello Mark,

The scenario you need to implement is supported with Telerik Data Access and the approach you chose is certainly in the right direction. To complete it, all you need to do is use the FlushChanges(bool releaseMemory) overload of the method. In your case, the value of the parameter should be true.

Let me know if you need additional information.


Regards,
Doroteya
Telerik
 
OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
 
0
Mark
Top achievements
Rank 1
answered on 29 Sep 2014, 08:25 AM
Hi,

This works.
I already played around with FlushChanges() but didn't notice the overloaded function. :-(

Thank you very much :)

Best regards
Mark

Tags
Development (API, general questions)
Asked by
Mark
Top achievements
Rank 1
Answers by
Doroteya
Telerik team
Mark
Top achievements
Rank 1
Share this question
or