非内容项的果园数据插入/删除 [英] Orchard data insert/delete for non content item
问题描述
我打算在数据库的单列表中插入/删除模块操作(使用 Orchard Rules-Action API)中的一些信息.执行此类任务的最佳方法是什么,即非内容项目的数据操作.我不想走创建内容类型"路线.我只是想在数据库中保留一些非内容数据并查询/删除它们.
I am intending to insert/delete some information in a module's Action (using Orchard Rules-Action API) in a one-column table in the database. What is the best way of doing such tasks i.e. Data manipulation of non-content items. I do not want to go by the "Create a content type" route. I simply want to persist some non-content data in the database and query/delete them.
namespace xyz.Models
{
public class Task
{
public virtual int ContentId { get; set; }
public virtual int Retries { get; set; }
}
}
<小时>
SchemaBuilder.CreateTable("Task",
table => table
.Column<int>("ContentId")
.Column<int>("Retries")
);
return 1;
<小时>
namespace Xyz.Services
{
public class TaskService : ITaskService
{
private readonly IRepository<Task> _taskRepository;
public TaskService(IRepository<Task> taskRepository)
{
_taskRepository = taskRepository;
}
public Task CreateTask(int contentId)
{
var task = new Task { ContentId = contentId };
_taskRepository.Create(task);
return task;
}
}
}
推荐答案
如果您的意思是创建一个没有 ContentPart
的表格"是非内容,那么只需在模型文件夹中创建您想要的模型:
If you mean "creating a table without ContentPart
" by non-content, then just create your desired model in the models folder :
public class MyRecord{
public virtual int Id { get; set; }
public virtual string FOO{ get; set; }
public virtual string BAR{ get; set; }
}
显然您必须在迁移中创建一个表,如下所示:
and obviously you must create a table in migration as following :
SchemaBuilder.CreateTable("MyRecord",
table => table
.Column<int>("Id", c => c.PrimaryKey().Identity())
.Column<string>("FOO")
.Column<string>("BAR")
);
最后,你想在表上进行事务,只需注入你模型的repository
的一个实例:
and finally where you want to have a transaction over table ,simply inject an instance of your model's repository
:
private readonly IRepository<MyRecord> _repository;
public SomeClass(IRepository<MyRecord> repository){
_repository = repository;
}
public SomeMethod(){
var record = new MyRecord();
//initialize your class here
_repository.Create(record);
}
重要要注意的是,您的记录类必须位于 Models 文件夹中,并且必须包含 Id 属性.
Important to note is that your record class must be in the Models folder and must contain an Id property.
这篇关于非内容项的果园数据插入/删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!