0

I'm experiencing an issue refreshing data in XPCollection after commiting some database changes using UnitOfWork.

I have a WinForm with an XPCollection.

The XPCollection uses XpoDefault.Session.

I do some changes through a UnitOfWork:

using (UnitOfWork uow = new UnitOfWork())
    {
    var photos = new XPCollection<Photo>(uow);
    photos[0].Date = DateTime.Now;
    uow.CommitTransaction();

    }

To get the original XPCollection to update the changes, I've tried the following:

foreach (Photo photo in myXPCollection)
{
XpoDefault.Session.Reload(photo);
}

foreach (Photo photo in myXPCollection)
{
photo.Reload();           
}

myXPCollection.Reload()

None of the methods work.The changes are not reflected in the original XPCollection.

They are only visible when I start with a completely new Session. Obviously, this is a big performance problem.

How to get the changes made using UnitOfWork to another Session?

SharpAffair
  • 5,558
  • 13
  • 78
  • 158

1 Answers1

1

You said:

They are only visible when I start with a completely new Session. Obviously, this is a big performance problem.

That's exactly what you should do. Create a new UnitOfWork each time you want refreshed data. A UnitOfWork is very cheap performance-wise to instantiate. If you have a large collection of Photo objects, you should improve performance by loading only the objects you need by specifying the Criteria parameter in the XPCollection<Photo> constructor.

When you issue Reload() it doesn't fetch anything from the database: it discards any changes and reloads the object from the session identity map. You can read this article and this Support Center issue for more information.

By the way, the DevExpress Support Center is by far the best place to ask DevExpress questions.

shamp00
  • 11,106
  • 4
  • 38
  • 81
  • Creating a new Session every time looks illogical to me. Isn't there a way to just force the existing XPCollection/Session to refresh from the data source? – SharpAffair Apr 09 '13 at 14:42
  • You shouldn't need to refresh from the data source. Oh I see, you're using the wrong commit. Change it to `uow.CommitChanges()`. – shamp00 Apr 09 '13 at 15:00
  • `CommitTransaction()` is for [transactions](http://documentation.devexpress.com/#XPO/CustomDocument2111). – shamp00 Apr 09 '13 at 15:01
  • Good find. But the changes are still not available with the original Session, even after CommitChanges in UnitOfWork. – SharpAffair Apr 09 '13 at 15:16
  • You're doing something wrong. Here's [a working sample program](https://gist.github.com/shamp00/5347869). – shamp00 Apr 09 '13 at 17:58