Import / Export Database

3 posts, 0 answers
  1. Mark
    Mark avatar
    14 posts
    Member since:
    Jul 2013

    Posted 25 Sep 2014 Link to this post

    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. Doroteya
    Admin
    Doroteya avatar
    502 posts

    Posted 29 Sep 2014 Link to this post

    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.
     
  3. DevCraft banner
  4. Mark
    Mark avatar
    14 posts
    Member since:
    Jul 2013

    Posted 29 Sep 2014 in reply to Doroteya Link to this post

    Hi,

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

    Thank you very much :)

    Best regards
    Mark

Back to Top