我已经创建了我想要收集于乌节路的数据库中一些简单的数据模块果园CMS数据库,所以我创建的模型,迁移和处理它:如何保存自定义数据
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);
}
...
}
现在
书面服务类为我的模块,当我尝试创建一个项目并将其保存在数据库中它trows an exeption:
attempted to assign id from null one-to-one property: ContentItemRecord
注即_stateRepository
是一个IRepository<StatePartRecord>
类型的注入对象。
什么是黄?
感谢您的回答,我已经尝试通过IContentManager.Create()来做到这一点,但它没有工作,并且“无效的Cast Exeption”无效。我可以请你给我一个例子吗? – 2012-08-10 10:00:01
我已经添加了一个。请注意,你的部分应该有一个驱动程序! – Piedone 2012-08-11 18:36:33
谢谢@Piedone。我添加了驱动程序,它完美的工作! 另一个问题是,如何使用ContentManager服务更新特定的contentitem? – 2012-08-14 08:17:15