我创建了一个模块,我想在 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
请注意,这_stateRepository
是一个IRepository<StatePartRecord>
类型化的注入对象。
什么是王?