如何从Orchard CMS中的数据库获取表数据? [英] How to fetch table data from database in Orchard CMS?

查看:502
本文介绍了如何从Orchard CMS中的数据库获取表数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从Orchard CMS中的数据库提取表数据?
我是Orchard CMS的初学者,我不知道如何获取和保存Orchard CMS中的任何数据,因为它不像一个常规的数据库访问模型像ado.net或linq到sql或实体框架。

How to fetch table data from database in Orchard CMS ? I am a beginner in Orchard CMS, I don't know to fetch and save any data in Orchard CMS, because it is not like a regular database accessing model like ado.net or linq to sql or entity framwork.

推荐答案

好吧,所以我建议先阅读文档并按照一些教程开始(例如 http://www.ideliverable.com/blog/writing-an -orchard-webshop-module-from-scratch-part-1 )。我至少发现果园有点艰难跳进来,不得不慢慢地慢慢地读,所有相关的材料。一旦你有一个想法,他会发生什么,最好的资源,以抓住Orchard的复杂性是在Orchards源代码中寻找例子。

Okay, so I would recommend starting off by reading the docs and following some tutorials (eg. http://www.ideliverable.com/blog/writing-an-orchard-webshop-module-from-scratch-part-1). I at least found Orchard a bit tough to jump into and had to sort of wade in slowly, reading all the relevant material. Once you have a bit of an idea about what the hell is going on, the best resource to get to grips with the intricacies of Orchard is to look for examples in Orchards source code.

无论如何,你在这里谈论两个不同的事情,访问Orchard内容项,并在Orchard中访问你自己的表。两者都比较简单。 Orchard有一堆好的帮助方法来做到这一点。

Anyway, you are talking about two different things here, accessing Orchard content items and accessing your own tables in Orchard. Both are relatively simple. Orchard has a bunch of nice helper methods to do so. For accessing Orchard content you will want to use the orchard content manager.

private readonly IOrchardServices services;

public AdminController(IOrchardServices services) {
   this.services = services;
}

所以我们现在在我们的控制器中有OrchardServices, ..

So we now have the OrchardServices in our controller and can use it like so...

var contentId = 12;
var contentItem = this.services.ContentManager.Get(contentId);

这将从指定的ID获取内容项。说如果你想得到所有的内容项目与一个名为MyPart的部分,你可以这样做:

This will get the content item from the specified id. Say if you wanted to get all content items with a part called MyPart, you could do this:

var items = this.services.ContentManager.Query<MyPart>().List();

它有许多其他方法来创建内容等。现在为了查询你自己的表,使用存储库。所以说你有一个名为FooRecord的模型,你可以这样做。

It has a bunch of other methods for creating content etc. Now for querying your own tables, you can use repositories. So say you have a model called FooRecord, you can do this.

private readonly IRepository<FooRecord> fooRepository;

public AdminController(IRepository<FooRecord> fooRepository) {
   this.fooRepository = fooRepository;
}

您可以使用如下的linq表达式查询:

You can query it using linq expressions like this:

var foo = fooRepository.Fetch(x => x.Id == fooId).FirstOrDefault();

要创建记录,您可以...

To create a record you can do...

var record = new FooRecord() { FooName = "Bob" };
this.fooRepository.Create(record);

Simples!希望这有助于您入门:)

Simples! Hope this helps get you started :)

编辑:

可以使用.As<>例如

So to get part data from a content item you can use .As<> eg.

var fooPart = contentItem.As<FooPart>();
var name = fooPart.Name;

这篇关于如何从Orchard CMS中的数据库获取表数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆