nhibernate - 使用 Orchard CMS 在迁移中创建新的内容项

标签 nhibernate orchardcms-1.6

这是我的迁移代码:

public Migrations(IRepository<ProductPartRecord> productPartRepository, IRepository<CategoryPartRecord> categoryPartRepository)
    {
        _productPartRepository = productPartRepository;
        _categoryPartRepository = categoryPartRepository;
    }

    public int Create() {
        ContentDefinitionManager.AlterTypeDefinition("Category", builder => builder
            .WithPart("CommonPart")
            .WithPart("TitlePart")
            .WithPart("AutoroutePart"));

        ContentDefinitionManager.AlterTypeDefinition("Category", builder => builder
            .WithPart("AutoroutePart", partBuilder => partBuilder
                .WithSetting("AutorouteSettings.AllowCustomPattern", "true")
                .WithSetting("AutorouteSettings.AutomaticAdjustmentOnEdit", "false")
                .WithSetting("AutorouteSettings.PatternDefinitions", "[{Name:'Category Title', Pattern: 'category/{Content.Slug}', Description: 'category/category-title'}]")));

        SchemaBuilder.CreateTable("CategoryPartRecord", table => table
            .ContentPartRecord()
                .Column<string>("Name")
                .Column<string>("Description")
                .Column<string>("Image")
            );

        ContentDefinitionManager.AlterTypeDefinition("Category", builder => builder
            .WithPart("CategoryPart"));

        ContentDefinitionManager.AlterTypeDefinition("Category", builder => builder
            .Creatable()
            .Draftable());

        return 1;
    }


    public int UpdateFrom1() {

        _categoryPartRepository.Create(new CategoryPartRecord { Name = "Category1", Description = "Description1", Image = "Image1" });

        return 2;
    }

UpdateFrom1 显然试图插入一条虚拟记录,但这会导致 nHibernate 抛出此异常:

“尝试从 null 一对一属性分配 id:ContentItemRecord”

零件记录如下所示:

public class CategoryPartRecord : ContentPartRecord {
    public CategoryPartRecord()
    {
        CategoryProducts = new List<CategoryProductRecord>();
    }

    public virtual string Name { get; set; }
    public virtual string Description { get; set; }
    public virtual string Image { get; set; }

    public virtual IList<CategoryProductRecord> CategoryProducts { get; set; }
}

有关于我哪里出错的线索吗?谷歌什么也没产生。

最佳答案

好的,所以您正在创建一个内容部分记录,而不是其中的内容项。您想要的是更多类似的东西:

var item = _orchardServices.ContentManager.New("Category").As<CategoryPart>();
item.Name = "Bobs Item"; // Something like that...
item.ContentItem.As<TitlePart>().Title = "Yay a title"; // This syntax may be wrong, I'm very tired
_orchardServices.ContentManager.Create(item);
_orchardServices.ContentManager.Publish(item.ContentItem);

我认为你会这样做。也许您想考虑使用导入/导出模块创建内容项,这是更常见和更安全的方法。

关于nhibernate - 使用 Orchard CMS 在迁移中创建新的内容项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16660117/

相关文章:

nhibernate - NHibernate:如何在亲子关系中强制生成 child 的ID

c# - Entity Framework 中是否存在 NHibernate.ToFuture() 扩展方法的模拟?

c# - Orchard 客户端验证 - 它应该如何外观/工作?

orchardcms - 自定义 Orchard cms 导航区的整个标记

error-handling - Orchard 电子邮件错误

c# - 没有 ContentPartRecord 的自定义部件的自定义排序标准 HQL 查询

linq - NHib 3 配置和映射返回空结果?

nhibernate - 强制休眠保存未更改的对象(将其标记为脏)

nhibernate - 在共享主机上使用 CaSTLe Windsor 和 NHibernate 设施

orchardcms - 在 Orchard 1.6 中导出绑定(bind)