问题
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;
}
}
}
回答1:
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")
);
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);
}
Important to note is that your record class must be in the Models folder and must contain an Id property.
来源:https://stackoverflow.com/questions/17426612/orchard-data-insert-delete-for-non-content-item