gpt4 book ai didi

c# - 如何在 Orchard CMS 数据库中保存自定义数据

转载 作者:行者123 更新时间:2023-12-04 05:36:22 25 4
gpt4 key购买 nike

我创建了一个模块,我想在 Orchard 的数据库中收集一些简单的数据,所以我为它创建了模型、迁移和处理程序:
Models/StatePartRecord.cs :

 namespace Address.Models
{
public class StatePart : ContentPart<StatePartRecord>
{
public int Id
{
get { return Record.Id; }
set { Record.Id = value; }
}
public string StateName
{
get { return Record.StateName; }
set { Record.StateName = value; }
}
}
}
Models/StatePartRecord.cs :
namespace Address.Models
{
public class StatePartRecord : ContentPartRecord
{
public virtual int Id { get; set; }
public virtual string StateName { get; set; }
}
}
Migrations.cs :
namespace Address
{
public class Migrations : DataMigrationImpl
{
public int Create()
{
SchemaBuilder.CreateTable("StatePartRecord", table => table
.ContentPartRecord()
.Column<string>("StateName")
);

return 1;
}
public int UpdateFrom1()
{
ContentDefinitionManager.AlterPartDefinition("State", part => part
.Attachable());

return 2;
}

}
}
Handlers/StatePartHandler.cs :
namespace Address.Handlers
{
public class StatePartHandler : ContentHandler
{
public StatePartHandler(IRepository<StatePartRecord> repository)
{
Filters.Add(StorageFilter.For(repository));
}
}
}

服务/MyService.cs :
namespace Address.Services
{
public class AddressService : IAddressService
{
...
public void InsertState(Models.StatePartRecord state)
{
_stateRepository.Create(state);
}
...
}

现在在我的模块的书面服务类中,当我尝试创建一个项目并将其保存在数据库中时,它会抛出一个异常:
attempted to assign id from null one-to-one property: ContentItemRecord

备注 _stateRepositoryIRepository<StatePartRecord>类型化的注入(inject)对象。

什么是王?

最佳答案

这是因为 ContentPartRecord 有一个 ContentItemRecord 属性,该属性指向与 ContentPartRecord 的 Part 附加到的内容项对应的 ContentItemRecord。

您不必直接管理零件记录:Orchard 服务(主要是 ContentManager)会为您完成这项工作。即使您想修改较低级别的记录,您也应该通过 ContentManager 进行此操作(通过注入(inject) IContentManager)。仅当它们只是用于存储非内容数据的“普通”记录时才直接操作记录,即不是 ContentPartRecords。

        // MyType is a content type having StatePart attached
var item = _contentManager.New("MyType");

// Setting parts is possible directly like this.
// NOTE that this is only possible if the part has a driver (even if it's empty)!
item.As<StatePart>().StateName = "California";

_contentManager.Create(item);

关于c# - 如何在 Orchard CMS 数据库中保存自定义数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11880078/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com