";不能保存不包含表示项ID“”的属性的类;当GlassCast(ing)一个分支模板项时,为什么

本文关键字:一个 ing 分支 为什么 GlassCast 包含 保存 不能 quot 表示 ID | 更新日期: 2023-09-27 18:20:10

我一整天都在想这个问题,我已经没有什么想法了。主要是因为glass.mapper似乎不支持分支模板,所以我使用标准的DB方法创建了一个sitecore项。然后我使用GlassCast<T>()给我一个正确类型的对象。

这个键入的对象似乎包含一个id字段,但是,当我尝试保存对象时,我会得到错误:

不能保存不包含属性的类表示项目ID

这是我的方法:

private Glass.Mapper.Sc.ISitecoreService SitecoreService;
public T CreateFromBranch<T>(string parentPath, string itemName,
            Guid branchTemplateId) where T : class, ICmsItem
{
    Item parent = SitecoreService.Database.GetItem(parentPath);//SitecoreService.GetItem<Item>(parentPath);
    if (parent == null)
        throw new NullReferenceException("Cannot load item to act as parent from path: " + parentPath);
    BranchItem branch = SitecoreService.Database.GetItem(new ID(branchTemplateId));
    if (branch == null)
        throw new ApplicationException("Failed ot find template branch with the id:" + branchTemplateId);
    Item itemFromBranch = parent.Add(itemName, branch);                   
    if (itemFromBranch == null)
        throw new ApplicationException("Failed to create item from branch template. BranchId: " + branchTemplateId);

    itemFromBranch.Fields.ReadAll();

    T typedItem  = itemFromBranch.GlassCast<T>();
    //Id is accessible here and is the id of the item that is created!
    Guid id = typedItem.Id;
    typedItem.DisplayName = itemDisplayName;
    //Exception thrown here!
    SitecoreService.Save(typedItem);
    return typedItem;
}

接口:

[SitecoreType(AutoMap = true)]
public interface ICmsItem
{
    //Id flagged here! Same in the concrete class
    [SitecoreId]
    Guid Id { get; }
    [SitecoreInfo(SitecoreInfoType.Name)]
    string Name { get; set; }
    [SitecoreInfo(SitecoreInfoType.DisplayName)]
    string DisplayName { get; set; }
    [SitecoreInfo(SitecoreInfoType.Path)]
    string Path { get; set; }
    [SitecoreInfo(SitecoreInfoType.TemplateId)]
    Guid TemplateId { get; set; }
    [SitecoreField(FieldName = "__Never publish")]
    bool NeverPublish { get; set; }
    [SitecoreField(FieldName = "__Icon")]
    string CmsIcon { get; set; }
    [SitecoreField(FieldName = "__Workflow")]
    Guid? Workflow { get; set; }
    [SitecoreField(FieldName = "__Workflow state")]
    Guid? WorkflowState { get; set; }
    [SitecoreField(FieldName = "__Sortorder")]
    int SortOrder { get; set; }
}

项目是在sitecore中创建的。但它没有更新的显示名称。如果我修改代码,使其根本不使用玻璃映射器:

if (!string.IsNullOrWhiteSpace(itemDisplayName))
{
     itemFromBranch.EditField("__Display name", itemDisplayName);
}

它有效!

有人知道我在这里做错了什么吗?

";不能保存不包含表示项ID“”的属性的类;当GlassCast(ing)一个分支模板项时,为什么

最终做到了。这个问题实际上并不包含缺失的部分,但我会给出一个答案,以防其他人被困在这个死胡同里!

我的问题实际上是具体的课堂。我没有注意到的是,这个类中包含了其他类:

public class V2Development : ICmsItem
{
    .....
    [SitecoreField]
    public virtual IEnumerable<CallToActionButtonBase> CallToActionButtons { get; set; }
}

CallToActionButtonBase类型没有ID字段!因此,glass mapper正在向下遍历整个依赖树,并试图解析所有对象。然后,当它遇到这个对象时,它失败了,因为它没有id(下次我们能有一个更具建设性的错误消息吗,请GlassMapper先生?比如说哪个对象缺少id?

我的解决方案是添加一层抽象,这样我就有了两个对象——一个有子对象,一个没有out。然后,我创建了一个没有子对象的对象实例(接口也会做类似的工作,但TBH,这个解决方案在接口中游刃有余,我试图避免再使用它)。

如果有人使用GlassMapper的可编辑助手类,而该类的模型属性没有关联的ID属性,则会出现同样的问题。解决这个问题的最简单方法是声明一个ID属性为&继承它(如果已经有基类型,则可以将它添加到基类型中)。

[SitecoreType]
public interface ISitecoreItem
{
    [SitecoreId]
    Guid ID { get; set; }
    [SitecoreInfo(Type = SitecoreInfoType.TemplateId)]
    Guid TemplateID { get; set; }
    [SitecoreInfo(Type = SitecoreInfoType.TemplateName)]
    string TemplateName { get; set; }
    [SitecoreInfo(Type = SitecoreInfoType.Path)]
    string Path { get; set; }
}

然后在组件/页面特定模型中继承以上内容。

public interface ISiteWide : ISitecoreItem
{
    //Component specific properties goes here 
}
相关文章: