非内容项的Orchard数据插入/删除

时间:2013-07-02 13:13:55

标签: orchardcms

我打算在数据库的单列表中插入/删除模块的Action(使用Orchard Rules-Action API)中的一些信息。执行此类任务的最佳方式是什么,即非内容项的数据处理。我不想通过“创建内容类型”路线。我只想在数据库中保留一些非内容数据并查询/删除它们。

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;
        }
   }
}

1 个答案:

答案 0 :(得分:4)

如果您的意思是“通过非内容创建没有ContentPart的表格”,那么只需在模型文件夹中创建所需的模型:

  public class MyRecord{

        public virtual int Id { get; set; }

        public virtual string FOO{ get; set; }

        public virtual string BAR{ get; set; }

    }

显然你必须在迁移中创建一个表,如下所示:

SchemaBuilder.CreateTable("MyRecord",
                table => table
                    .Column<int>("Id", c => c.PrimaryKey().Identity())
                    .Column<string>("FOO")
                    .Column<string>("BAR")
                ); 

最后你想要在表上进行交易,只需注入模型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属性。