Search code examples
orchardcmsbackground-task

Orchard background task not persisting PartRecords to the database


I'm trying to use a background task to gather Likes/Comments from the Facebook Graph APi and use that to drive our blog's trending.

Here the trendingModels have already been populated and are being used to fill in the TrendingParts.GraphId and TrendingParts.TrendingValue.

I'm not getting any exceptions and the properties on TrendingPart point to the fields in the TrendingPartRecord.

Yet nothing persists to the database, any ideas why?

_orchardsServices is IOrchardServices

var articleParts = _orchardService.ContentManager.GetMany<TrendingPart>(
                                                            trendingModels.Select(r => r.OrchardId).ToList(), 
                                                            VersionOptions.Published,
                                                            QueryHints.Empty);

        // Cycle through the records and update them from the matching model
        foreach (var articlePart in articleParts) 
        {
            ArticleTrendingModel trendingModel = trendingModels.Where(r => r.OrchardId == articlePart.Id).FirstOrDefault();

            if(trendingModel != null)
            {
                // Not persisting to the database, WHY?
                // What's missing?
                // If I'm understanding things properly nHibernate should push this to the db autoMagically.
                articlePart.GraphId = trendingModel.GraphId;
                articlePart.TrendingValue = trendingModel.TrendingValue;
            }
        }

Edit: It's probably worth noting that I can update and publish the fields on the TrendingPart in the admin panel but the saved changes don't appear in the MyModule_TrendingPartRecord table.


Solution

  • The solution was to change my Service to a transient dependency using ITransientDependency.

    The service was holding a reference to the PartRecords array and because it was treated as a Singleton it never disposed and the push to the database was never made.