How to: Generate XPO Business Classes for Existing Data Tables 如何:为现有数据表生成 XPO 业务类
From the Tutorial and other documentation sources, you learned how to create business classes for your XAF applications. If you have business classes in your application, you have database tables in the application's database. However, the reality is that most programmers are not building new applications from scratch, but maintaining existing databases. In this instance, they can use the XPO Data Model Wizard that generates a business model declaration for the specified legacy database. Follow the steps below to generate business classes for your existing database that you are going to use in your XAF application.
通过教程和其他文档来源,您学习了如何为 XAF 应用程序创建业务类。如果应用程序中有业务类,则应用程序的数据库中有数据库表。但是,现实情况是,大多数程序员不是从头开始构建新应用程序,而是维护现有数据库。在这种情况下,他们可以使用 XPO 数据模型向导为指定的旧数据库生成业务模型声明。按照以下步骤为要在 XAF 应用程序中使用的现有数据库生成业务类。
If you prefer to watch a video rather than walk through these step-by-step instructions, you can access a corresponding tutorial in our YouTube Channel: XAF: Create an Application Based on the Existing Database
如果您更喜欢观看视频,而不是演练这些分步说明,您可以在我们的 YouTube 频道访问相应的教程:XAF:基于现有数据库创建应用程序
Generate an XPO Data Model
- Create a new XAF solution using the DevExpress v19.2 XAF Solution Wizard template.
- Right-click the BusinessObjects folder located in the module project. Choose Add | New Item. In the invoked Add New Item dialog, choose the DevExpress ORM Data Model Wizard template located in the DevExpress category. Set the new item's name to MySolutionDataModel.xpo and click Add. You will see that the MySolutionDataModel.xpo item is added and the wizard dialog is invoked.
In the invoked wizard dialog, choose Map to an existing database and click Next.
生成 XPO 数据模型
使用 DevExpress v19.2 XAF 解决方案向导模板创建新的 XAF 解决方案。
右键单击模块项目中的"业务对象"文件夹。选择"添加 |新项目。在调用的"添加新项目"对话框中,选择位于"开发人员快递"类别中的 DevExpress ORM 数据模型向导模板。将新项目的名称设置为 MySolutionDataModel.xpo,然后单击"添加"。您将看到 MySolutionDataModel.xpo 项已添加,并调用向导对话框。
在调用的向导对话框中,选择"映射到现有数据库",然后单击"下一步"。

Specify connection settings to the database containing the target data. Multiple database systems (MS SQL Server, DB2, MySql, Firebird, etc) are supported by the wizard. Use the Provider combo box to select the required database type. Note that the corresponding database provider assembly must be registered in the Global Assembly Cache (GAC) on your machine or the wizard will fail. In this example, we will use the "Northwind Traders" demo database. This database is shipped with DXperience and installed in %PUBLIC%\Documents\DevExpress Demos 19.2 \Components\Data\nwind.mdb, by default.
指定到包含目标数据的数据库的连接设置。向导支持多个数据库系统(MS SQL 服务器、DB2、MySql、Firebird 等)。使用提供程序组合框选择所需的数据库类型。请注意,必须在计算机上的全局程序集缓存 (GAC) 中注册相应的数据库提供程序程序集,否则向导将失败。在此示例中,我们将使用"北风交易者"演示数据库。默认情况下,此数据库随 DXperience 一起提供,并安装在 %PUBLIC%_文档_DevExpress 演示 19.2 [组件]数据nwind.mdb 中。

Click Next after connection settings have been specified.
指定连接设置后,单击"下一步"。
In the next step, the wizard displays a list of tables that can be mapped to persistent classes. Select a table(s) to be mapped to a persistent object(s) and for each table select columns that will be mapped to the persistent object's properties. For unchecked columns, persistent properties will not be generated. For instance, select the Customers and Orders tables.
在下一步中,向导显示可映射到持久类的表的列表。选择要映射到持久对象的表,为每个表选择将映射到持久对象属性的列。对于未选中的列,将不会生成持久属性。例如,选择"客户"和"订单"表。

Click Next to close the wizard. The generated data model will be shown in the XPO Data Model designer.
单击"下一步"关闭向导。生成的数据模型将显示在 XPO 数据模型设计器中。

If you want to customize the generated data model, refer to the How to: Create a Business Model in the XPO Data Model Designer topic. The names of persistent classes and their properties match the names of selected tables and their columns. In the current sample, the table names are in plural form. So, you may want to change class names (Customers to Customer and Orders to Order). To rename a class or its property, select it in the designer and change the Name in the Properties window. The classes and properties with modified names will still be mapped to corresponding tables and columns, as the PersistentAttribute is automatically added to the designer-generated code.
如果要自定义生成的数据模型,请参阅 XPO 数据模型设计器主题中的"如何:创建业务模型"。持久性类的名称及其属性与所选表及其列的名称匹配。在当前示例中,表名称为复数形式。因此,您可能需要更改类名称(客户到客户和订单到订单)。要重命名类或其属性,请在设计器中选择它,并在"属性"窗口中更改"名称"。由于持久性属性自动添加到设计器生成的代码中,因此具有修改名称的类和属性仍将映射到相应的表和列。
In the Visual Studio toolbar, click save. The generated code files will appear in the Solution Explorer, in the BusinessObjects\MySolutionDataModelCode folder.
在 Visual Studio 工具栏中,单击"保存"。生成的代码文件将显示在"解决方案资源管理器"中的"业务对象"_MySolutionDataModel 代码文件夹中。

Note 注意
If you do not like to deal with the designer and prefer to do everything in code, create a separate code file and copy the generated classes into it. Then, remove files that are added by the designer.
如果您不喜欢处理设计器,并且喜欢在代码中执行所有操作,请创建一个单独的代码文件并将生成的类复制到其中。然后,删除设计器添加的文件。
Add XAF-Specific Attributes in Code
在代码中添加特定于 XAF 的属性
Open the Customer.cs (Customer.vb) file. Decorate the Customer class with the DefaultClassOptionsAttribute and ImageNameAttribute attributes. As the result, the Customer object will be added to the Navigation System and an icon from the built-in library will be associated with this object.
打开Customer.cs(Customer.vb)文件。使用默认类选项属性和图像名称属性属性装饰客户类。因此,客户对象将添加到导航系统,并且内置库中的图标将与此对象关联。
using DevExpress.Persistent.Base;
// ...
[DefaultClassOptions, ImageName("BO_Contact")]
public partial class Customer{
public Customer(Session session) : base(session) { }
public Customer() : base(Session.DefaultSession) { }
public override void AfterConstruction() { base.AfterConstruction(); }
}Open the Order.cs (Order.vb) file. Decorate the Order class with the DefaultClassOptions and ImageName attributes.
打开Order.cs(Order.vb)文件。使用默认类选项和图像名称属性装饰 Order 类。
using DevExpress.Persistent.Base;
// ...
[DefaultClassOptions, ImageName("BO_Order")]
public partial class Order {
public Order(Session session) : base(session) { }
public Order() : base(Session.DefaultSession) { }
public override void AfterConstruction() { base.AfterConstruction(); }
}
You can add more custom code to the auto-generated classes (e.g., override base class methods). Do not change code located in files with the designer suffix - they contain designer-generated code and should not be modified manually. The generated classes are declared as partial. Designed and custom class parts are located in different files.
您可以向自动生成的类添加更多自定义代码(例如,重写基类方法)。不要更改位于具有设计器后缀的文件中的代码 - 它们包含设计器生成的代码,不应手动修改。生成的类声明为部分类。设计和自定义类零件位于不同的文件中。
Note 注意
It is recommended that you specify the default property for each generated class via the System.ComponentModel.DefaultProperty attribute. Default property is treated as a human-readable identifier in an XAF application UI.
建议您通过 System.组件模型.DefaultProperty 属性为每个生成的类指定默认属性。默认情况下属性在 XAF 应用程序 UI 中被视为人类可读的标识符。
Important 重要
You cannot apply attributes to properties in the partial class' code. Instead, use the designer (see the next section).
不能将属性应用于部分类代码中的属性。而是使用设计器(请参阅下一节)。
Add XAF-Specific Attributes in the Designer
在设计器中添加 XAF 特定属性
Alternatively, you can use the designer to apply attributes. Focus the requires class or field and specify the Custom Attributes setting in the Properties window.
或者,可以使用设计器应用属性。聚焦需要类或字段,并在"属性"窗口中指定"自定义属性"设置。

Specify the Connection String
指定连接字符串
An empty XAF application is generated with a default connection string: Data Source=.\SQLEXPRESS;Initial Catalog=MyApplication;Integrated Security=SSPI;Pooling=false. You should change it, so that the application uses the required database. Change the connection string in the application configuration file. You can copy the valid connection string from the auto-created BusinessObjects\MySolutionDataModelCode\ConnectionHelper.cs (ConnectionHelper.vb) file.
使用默认连接字符串生成空 XAF 应用程序:数据源sqlEXPRESS;初始目录_我的应用程序;集成安全性=SSPI;池_false。应更改它,以便应用程序使用所需的数据库。更改应用程序配置文件中的连接字符串。可以从自动创建的业务对象[MySolutionDataModelCode_连接帮助程序.cs(连接帮助器.vb)》文件中复制有效的连接字符串。
Run the Application
Now you can run the WinForms, ASP.NET and Mobile applications to see the result. These applications are completely based on the business model generated for the legacy database.
- WinForms
- ASP.NET
- Mobile
运行应用程序
现在,您可以运行 WinForms、ASP.NET和移动应用程序以查看结果。这些应用程序完全基于为旧数据库生成的业务模型。
- WinForms
- ASP.NET
- 移动

How to: Generate XPO Business Classes for Existing Data Tables 如何:为现有数据表生成 XPO 业务类的更多相关文章
- Implement Custom Business Classes and Reference Properties 实现自定义业务类和引用属性(XPO)
In this lesson, you will learn how to implement business classes from scratch. For this purpose, the ...
- How to: Handle Renamings and Deletions of Business Classes and their Properties 如何:处理业务类及其属性的重命名和删除
When developing an XAF application, you may be required to rename a persistent class or property due ...
- Implement Custom Business Classes and Reference Properties实现自定义业务类和引用属性(EF)
In this lesson, you will learn how to implement business classes from scratch. For this purpose, the ...
- XAF-从业务类继承 (XPO)
In this lesson, you will learn how to implement business classes for your application using the Busi ...
- Data Being Added Conflicts with Existing Data
While developing a page with multiple scrolls levels, and especially when using a grid, you may get ...
- 17.1.1.8?Setting Up Replication with Existing Data设置复制使用存在的数据
17.1.1.8?Setting Up Replication with Existing Data设置复制使用存在的数据 当设置复制使用存在的数据,你需要确定如何最好的从master 得到数据到sl ...
- valueError: This solver needs samples of at least 2 classes in the data, but the data contains only one class: 0
问题描述:执行下面的代码,报错valueError: This solver needs samples of at least 2 classes in the data, but the data ...
- numpy 基于现有数据创建ndarray(from existing data)
1 numpy.array array(object[, dtype=None, copy=True, order='K', subok=False, ndmin=0]) 2 numpy.asarra ...
- TMF大数据分析指南 Unleashing Business Value in Big Data(一)
大数据分析指南 TMF Frameworx最佳实践 Unleashing Business Value in Big Data 前言 此文节选自TMF Big Data Analytics Guide ...
随机推荐
- Android 中的Activity、Window、View之间的关系
一.概述 Activity 可以说是应用程序的载体(也可以理解为界面的载体,但是不界面),用户能够在上面绘制界面(Activity本身不绘制界面),并提供用户处理事件的API,维护应用程序的生命周 ...
- shell du sh 文件大小输出
按照文件大小升序输出结果: du -sh * | sort -h 如果要逆序输出,则: du -sh * | sort -hr
- 剑指offer-31:整数中1出现的次数(从1到n整数中1出现的次数)
参考: https://troywu0.gitbooks.io/interview/整数中出现1的次数(从1到n整数中1出现的次数).html 题目描述 求出1~13的整数中1出现的次数,并算出100 ...
- Android多图选择
多图选择是Android中一个常用的功能,用户可以拍照或者批量选择图片上传,还是国际惯例,先看下效果图,demo地址我会放到文章末尾. 经过对比,这里我选择了一个第三方开源库PictureSelect ...
- iOS UILable和属性字符串的使用
UILable的常用方法和属性 设置文字颜色(默认为黑色) @property(nonatomic,strong) UIColor *textColor 设置显示文字 @property(no ...
- EditText 无法失焦与失焦后键盘不收缩解决方案
背景 有一个需求,比方说下图: 点击了上图的Image 区域才可以编辑. 那么我首先想到的就是: android:focusable="false" 不让它获取到焦点不就ok吗? ...
- dubbo 发布 RPC 服务
Dubbo 发布 RPC 服务 建立服务提供者项目 pom.xml <?xml version="1.0" encoding="UTF-8"?> & ...
- Zookeeper Watcher接口
在ZooKeeper中,接口类Watcher用于表示一个标准的事件处理器,其定义了事件通知相关的逻辑,包含KeeperState和EventType两个枚举类,分别代表了通知状态和事件类型,同时定义了 ...
- 线性规划VB求解
线性规划VB求解 Rem 定义动态数组 Dim a() As Single, c() As Single, b() As Single, cb() As Single Dim aa() As Sing ...
- 区块链学习——HyperLedger-Fabric v1.0环境搭建详细教程
相对与v0.6版本来说,1.0版本改变较大,此处不多说,只是将小白自己搭建1.0环境的过程分享给大家.希望对大家能有所帮助! 这一篇可能对前面的环境搭建会写的有些粗略,如有疑问,可阅读上一篇V0.6版 ...