I have a reconciliation process where by a background thread that periodically retrieves a list of object ids from an external webservice and attempts to add the missing entities to an embedded RavenDb database. The loop that performs this process is the following:
foreach (var pageId in listOfPageIds)
{
if ( _contentService.GetPageByPageId(pageId) == null)
{
_contentService.AddPage(pageId);
}
}
the implementation of the GetPageByPageId()
and AddPage()
are as follows:
public Page GetPageByPageId(string pageId)
{
using (var session = DocumentStore.OpenSession())
{
return session.Query<Page>().FirstOrDefault(page => page.PageId == pageId);
}
}
public bool AddPage(string pageId)
{
var page = GetPageByPageId(pageId);
if (page != null)
{
return false;
}
using (var session = DocumentStore.OpenSession())
{
var newPage = new Page() {PageId = pageId};
session.Store(newPage);
session.SaveChanges();
}
return true;
}
The problem is that if the list has duplicate ids, once it adds the first id and checks for that id again, the result comes back as empty. It is as if a finalization step is missing that would register the newly added entity. If I query the set from a different thread at a later time, the entity with that given id is returned. Can anyone see what the issue is here?
thanks,