Orchardcms 创建内容项后立即检索其最新版本及相关记录

Orchardcms 创建内容项后立即检索其最新版本及相关记录,orchardcms,Orchardcms,我正在构建一个模块,其中数据具有父子关系。我已经创建了一个服务,它使用一种方法来创建新的父记录及其子记录。方法如下所示: public ParentPart CreateParent(...) // Create parent content item var parentPart = _contentManager.New("Parent").As<ParentPart>(); parentPart.PropertyA = "value..."; _contentM

我正在构建一个模块,其中数据具有父子关系。我已经创建了一个服务,它使用一种方法来创建新的父记录及其子记录。方法如下所示:

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>();
}
public ParentPart CreateParent(…)
//创建父内容项
var parentPart=_contentManager.New(“Parent”).As();
parentPart.PropertyA=“值…”;
_contentManager.Create(parentPart);
//创建子对象
var childRecord=新的childRecord{
ChildPropertyA=“值…”,
ParentPartRecord=parentPart.Record
};
_childRepository.Create(childRecord);
//重新获取父部件以尝试获取相关子记录
return_contentManager.Get(parentPart.Id).As();
}
以下是part和record类的外观:

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; }
}
公共类ParentPart:ContentPart{
公共字符串属性a{
获取{return Retrieve(r=>r.PropertyA);}
设置{returnstore(r=>r.PropertyA,value);}
}
可数儿童的公共教育{
得到{
返回记录。儿童;
}
}
}
公共类ParentPartRecord:ContentPartRecord{
公共虚拟字符串属性{get;set;}
公共虚拟IList子项{get;set;}
}
公共班级儿童档案{
公共虚拟整数Id{get;set;}
公共虚拟字符串ChildPropertyA{get;set;}
公共虚拟ParentPartRecord ParentPartRecord{get;set;}
}
问题是,在调用我的服务的CreateParent方法后,返回的ParentPart的子集合为null。我非常确定关系设置正确,因为在任何后续请求中,我都可以检索内容项并访问子记录


似乎内容项的记录直到我请求孩子们之后才会更新。是否有办法强制执行此操作,以便我可以在调用此方法后立即检索子项,而无需等待下一个请求?

我发现,在重新获取内容项之前调用ITransactionManager.RequireRenew()确实会返回填充了子项的ParentPart。我很想知道其他人是否认为这是一个合适的解决方案,或者它是否会产生意外的副作用。你能解释一下具体的情况吗?很少有必要这样做。@BertrandLeRoy我已经为上下文添加了part和record类的示例。它们是对我实际代码的简化,但也存在同样的问题。这实际上归结为这样一个事实:在我的服务上调用CreateParent之后,ParentPart的Children属性立即为null。然而,当我在下一个请求中检查它时,它具有我期望的ChildRecord。在重新获取ParentPart(CreateParent方法的最后一行)之前调用ITransactionManager.RequireRenew()似乎可以解决此问题。谢谢,我很高兴您能让它工作,但我不是这么要求的。问题仍然是关于超抽象的“子”和“父”部分。我想知道你为什么不使用其他许多建立父子关系的方法之一,比如内容选择器字段、分类法或列表?哦,我完全误解了你的问题。我的实际类不称为parent和child(这些只是示例),也不打算在任何地方使用,除了自定义模块中的这个非常特殊的情况。实际的类正在对一种非常特定的请求类型(父类)进行建模,该类型上有多个行项目(子类)。与采购订单上可能出现的行项目类似。事实上,我曾考虑过使用你的nwaze.Commerce模块,但我的案例中有太多的细节,不符合典型的采购订单。