Sql server Orchard CMS连接外部SQL Server
我计划创建一些页面,显示来自Orchard CMS外部SQL Server的数据。看起来我需要编写一个新模块来实现这个功能。是否有任何示例或想法来实现此要求?是的,您需要编写一个新模块,该模块提供新的内容部分和内容部分驱动程序。该驱动程序将负责从外部SQL Server获取数据,您将外部SQL Server设置为从驱动程序返回的形状的属性。然后形状的视图将显示您的数据 本教程将指导您如何编写自定义内容零件: 执行此操作时,请确保不创建内容部分记录类型,因为您不会从Orchard数据库存储和加载数据-您希望从外部数据库加载数据。以下是您应该遵循的步骤:Sql server Orchard CMS连接外部SQL Server,sql-server,content-management-system,orchardcms,external,Sql Server,Content Management System,Orchardcms,External,我计划创建一些页面,显示来自Orchard CMS外部SQL Server的数据。看起来我需要编写一个新模块来实现这个功能。是否有任何示例或想法来实现此要求?是的,您需要编写一个新模块,该模块提供新的内容部分和内容部分驱动程序。该驱动程序将负责从外部SQL Server获取数据,您将外部SQL Server设置为从驱动程序返回的形状的属性。然后形状的视图将显示您的数据 本教程将指导您如何编写自定义内容零件: 执行此操作时,请确保不创建内容部分记录类型,因为您不会从Orchard数据库存储和加载数
ContentPart
,而不是ContentPart
,因为不会有任何“TRecord”Display
方法中,通过调用ContentShape
方法返回形状确保在lambda中添加SQL数据访问逻辑。如果您在lambda之外执行此操作,则每次调用使用内容部件的内容项时都会调用该数据访问代码。虽然这听起来似乎正是您想要的,但这里有一个微妙之处,涉及Placement.info,您可以使用它来确定何时实际渲染形状。如果放置逻辑确定不应渲染形状,则您不希望免费访问外部数据IDependency
一些示例伪代码:
服务代码:
public interface IMyExternalDataStore : IDependency {
IList<MyExternalDataRecord> GetMyData();
}
public class MyExternalDataStore : IMyExternalDataStore {
public IList<MyExternalDataRecord> GetMyData() {
// Connect to your SQL Server database, perhaps using EF, load the data and return it. Could of course also be simply a DataSet.
}
}
内容部分驱动程序:
public class MyExternalDataPartDriver : ContentPartDriver<MyExternalContentPart> {
private readonly IMyExternalDataStore _dataStore;
public MyExternalDataPartDriver(IMyExternalDataStore dataStore) {
_dataStore = dataStore;
}
protected override DriverResult Display(SlideShowProPart part, string displayType, dynamic shapeHelper) {
return ContentShape("Parts_MyExternalData", () => {
// Notice that we're performing the data access here within the lambda (the so called "shape factory method").
var data = _dataStore.GetMyData();
// Notice that I'm creating a property called "MyData"on the shape (which is a dynamic object).
return shapeHelper.Parts_MyExternalData(MyData: data));
}
}
}
谢谢你,西普。很好的向导,试着跟上你的逻辑。没问题!如果您需要有关特定零件的更多详细信息,请告诉我。
public class MyExternalDataPartDriver : ContentPartDriver<MyExternalContentPart> {
private readonly IMyExternalDataStore _dataStore;
public MyExternalDataPartDriver(IMyExternalDataStore dataStore) {
_dataStore = dataStore;
}
protected override DriverResult Display(SlideShowProPart part, string displayType, dynamic shapeHelper) {
return ContentShape("Parts_MyExternalData", () => {
// Notice that we're performing the data access here within the lambda (the so called "shape factory method").
var data = _dataStore.GetMyData();
// Notice that I'm creating a property called "MyData"on the shape (which is a dynamic object).
return shapeHelper.Parts_MyExternalData(MyData: data));
}
}
}
@{
var records = (IList<MyExternalDataRecord>)Model.MyData;
}
<ul>
@foreach(var record in records) {
<li>@record.ToString()</li>
}
</ul>
<Placement>
<Place Parts_MyExternalData="Content:0"/>
</Placement>
public class Migrations : DataMigrationImpl {
public int Create() {
// Define your content part so that you can attach it to any content type from the UI.
ContentDefinitionManager.AlterPartDefinition("MyExternalDataPart", part => part.Attachable());
// Optionally, define a new content type here programmatically or attach it to an existing type.
return 1;
}
}