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

how to check if there are uncommitted changes?

1 Answer 97 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.
Remco
Top achievements
Rank 1
Remco asked on 27 Nov 2013, 08:39 AM

Hello,

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

1 Answer, 1 is accepted

Sort by
0
Doroteya
Telerik team
answered on 29 Nov 2013, 12:43 PM
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>()
    .Concat(changes.GetUpdates<Journal>())
    .Concat(changes.GetDeletes<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.


Regards,
Doroteya
Telerik
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!
Tags
Development (API, general questions)
Asked by
Remco
Top achievements
Rank 1
Answers by
Doroteya
Telerik team
Share this question
or