I'm building a module where the data has a parent child relationship. I've created a service with a method to create new parent records along with their children. The method looks something like this:
public ParentPart CreateParent(...)
// Create parent content item
var parentPart = _contentManager.New("Parent").As<ParentPart>();
parentPart.PropertyA = "value...";
_contentManager.Create(parentPart);
// Create child
var childRecord = new ChildRecord {
ChildPropertyA = "value...",
ParentPartRecord = parentPart.Record
};
_childRepository.Create(childRecord);
// Re-fetch parent part to try and get related child records
return _contentManager.Get(parentPart.Id).As<ParentPart>();
}
Here's what the part and record classes look like:
public class ParentPart : ContentPart<ParentPartRecord> {
public string PropertyA {
get { return Retrieve(r => r.PropertyA); }
set { return Store(r => r.PropertyA, value); }
}
public IEnumerable<ChildRecord> Children{
get {
return Record.Children;
}
}
}
public class ParentPartRecord : ContentPartRecord {
public virtual string PropertyA { get; set; }
public virtual IList<ChildRecord> Children { get; set; }
}
public class ChildRecord {
public virtual int Id { get; set; }
public virtual string ChildPropertyA { get; set; }
public virtual ParentPartRecord ParentPartRecord { get; set; }
}
The problem is, after calling the CreateParent method of my service, the returned ParentPart's Children collection is null. I'm pretty sure the relationship is setup correctly since on any subsequent request, I can retrieve the content item and access the child records just fine.
It seems like content item's record is not updating until sometime after I'm requesting the children. Is there a way to force this so I can retrieve the children immediately after calling this method without waiting for the next request?