how to check if there are uncommitted changes?

Thread is closed for posting
2 posts, 0 answers
  1. Remco
    Remco avatar
    4 posts
    Member since:
    Oct 2013

    Posted 27 Nov 2013 Link to this post


    i am trying to change our code from EF4.0 to ORM  
    I have problem to found the correct way to replace the following EF Command:

    var uncommitedJournals = DbContext.ObjectStateManager.GetObjectStateEntries(EntityState.Added).Where(ent => ent.Entity is Journal).Select(ent => ent.Entity as Journal);
    if (uncommitedJournals.Any())
       // there are unsaved journal entries
       // ...

    What is the correct way to get the same functionality with ORM ?

    thanks in advance for any Help
  2. Doroteya
    Doroteya avatar
    498 posts

    Posted 29 Nov 2013 Link to this post

    Hello Remco,

    Telerik OpenAccess ORM offers the GetChanges() method of the OpenAccessContext class that can help you in this case. It basically retrieves all the changes that will be persisted during the next commit to an instance of the Telerik.OpenAccess.ContextChanges class and from there you can analyze them with the help of the GetInserts<T>(), GetUpdates<T>() and GetDeletes<T>() methods (here is an article that provides an example).

    Regarding the case provided by your code snippet, I would suggest to you the following:
    using (EntitiesModel dbContext = new EntitiesModel())
        Telerik.OpenAccess.ContextChanges changes = dbContext.GetChanges();
        IEnumerable<Journal> uncommittedJurnals = changes.GetInserts<Journal>();
        if (uncommittedJurnals.Any())
            //do the processing here

    You could also combine all the changes related to a given persistent type like this:
    IEnumerable<Journal> uncommittedJurnals = changes.GetInserts<Journal>()

    Note that OpenAccess ORM offers the so called persistence by reachability that allows you persist a graph of related objects to the database. You take advantage of it in the cases when you create two new object, relate them through the navigation properties and add just one of them to the Add() method of the context. In such scenarios both objects will be persisted to the database but only the one added to the context will be detected as a change by the GetChanges() method. To avoid that you need to take advantage of the IsManaged property of the navigation properties (this section of our documentation will give you additional information).

    I hope this helps. If you need further assistance or have other questions, do not hesitate to get back to us.

    OpenAccess ORM Q3 2013 simplifies your model operations even further providing you with greater flexibility. Check out the list of new features shipped with our latest release!
Back to Top