This topic provides step-by-step instructions on how to use the XPO Data Model Designer in XAF applications. We will create a simple business model consisting of two objects - Employee and Task. These objects will be linked with a one-to-many relationship. Then you will add XAF-specific attributes in code, and the application will be ready to use.
本主题提供有关如何在 XAF 应用程序中使用 XPO 数据模型设计器的分步说明。我们将创建一个由两个对象组成的简单业务模型 - 员工和任务。这些对象将关联一对多关系。然后在代码中添加特定于 XAF 的属性,应用程序将可以使用。
Note注意
This designer can also be used to generate a data model for a legacy database (see How to: Generate XPO Business Classes for Existing Data Tables).
此设计器还可用于为旧数据库生成数据模型(请参阅:如何为现有数据表生成 XPO 业务类)。
If you prefer to watch a video rather than walk through these step-by-step instructions, you can find the corresponding tutorial on the DevExpress YouTube Channel: XAF: Start from Building a Data Model using the XPO Data Model Designer
如果您更喜欢观看视频,而不是演练这些分步说明,您可以在 DevExpress YouTube 频道上找到相应的教程:XAF:从使用 XPO 数据模型设计器构建数据模型开始
Create 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 and choose Add | New Item. In the invoked Add New Item dialog, select 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 dialog, select Do not connect to a database and click Next, to skip the creation of a database connection using the XPO Data Model Wizard. The database connection is managed by XAF in your solution.
创建 XPO 数据模型
-
使用 DevExpress v19.2 XAF 解决方案向导模板创建新的 XAF 解决方案。
-
右键单击模块项目中的"业务对象"文件夹,然后选择"添加 |新项目。在调用的"添加新项目"对话框中,选择位于"开发人员快递"类别中的 DevExpress ORM 数据模型向导模板。将新项目的名称设置为 MySolutionDataModel.xpo,然后单击"添加"。您将看到 MySolutionDataModel.xpo 项已添加,并调用向导对话框。
-
在调用的对话框中,选择"不连接到数据库",然后单击"下一步",以跳过使用 XPO 数据模型向导创建数据库连接。数据库连接由解决方案中的 XAF 管理。
-
As a result, an empty data model will be shown in the designer.
-
因此,设计器中将显示空数据模型。
Design the Employee Object
设计员工对象
-
To add a new business class, drag the XpObject item from the toolbox to the designer's surface.
-
要添加新的 Business 类,请将 XpObject 项从工具箱拖动到设计器的曲面。
-
Focus the newly added object. In the Properties window, set the object's Name to Employee.
-
聚焦新添加的对象。在"属性"窗口中,将对象的"名称"设置为"员工"。
-
To add a new persistent property, drag the Field item from the Toolbox to the Employee object.
-
要添加新的持久属性,请将"字段"项从"工具箱"拖动到"员工"对象。
-
Focus the newly added field. In the Properties window, set the field's Name to FirstName.
-
聚焦新添加的字段。在"属性"窗口中,将字段的名称设置为"名字"。
Note that the Column Type is String by default.
请注意,默认情况下列类型是字符串。
-
Repeat the two previous steps to add the LastName property.
-
重复上述两个步骤以添加"姓氏"属性。
-
Now add a field of a type other than string. Add the Birthday field, and set its Column Type to DateTime.
-
现在添加字符串以外的类型的字段。添加"生日"字段,并将其列类型设置为"日期时间"。
-
Next, you will see how to add a calculated field. Drag the Persistent Alias item from the Toolbox to the Employee object.
-
接下来,您将看到如何添加计算字段。将"持久别名"项目从"工具箱"拖动到"员工"对象。
-
Focus the newly added persistent alias. In the Properties window, set Name to FullName. Then, click the ellipsis button located to the right of the Expression setting. In the invoked Expression editor dialog, specify the [FirstName] + ' ' + [LastName] expression and click OK.
-
聚焦新添加的持久别名。在"属性"窗口中,将"名称"设置为"全名"。然后,单击表达式设置右侧的省略号按钮。在调用的表达式编辑器对话框中,指定 [名字] = ' ' ' [ [姓氏] 表达式,然后单击"确定"。
Note 注意
You can inherit the Employee class from the DevExpress.Persistent.BaseImpl.Person class, instead of creating it from scratch. The XPO Data Model Designer supports the use of persistent classes from external assemblies as base classes and property types. To add business classes from the Business Class Library (or your own class library), do the following.
可以从 DevExpress.持久.BaseImpl.Person 类继承员工类,而不是从头开始创建它。XPO 数据模型设计器支持将外部程序集中的持久类用作基类和属性类型。要从 Business 类库(或您自己的类库)添加业务类,请执行以下操作。
Right-click the free space on the design surface. In the invoked context menu, choose Add Assembly.
Select the assembly that contains business classes and click Open. You can choose the DevExpress.Persistent.BaseImpl.v19.2.dll assembly that ships with XAF, located in the %PROGRAMFILES(x86)%DevExpress 19.2ComponentsBinFramework path, or your custom assembly.
In the Select Types dialog, choose the persistent classes to import from the assembly and click OK.
右键单击设计图面上的可用空间。在调用的上下文菜单中,选择"添加程序集"。
选择包含业务类的程序集,然后单击"打开"。您可以选择与 XAF 一起附带的 DevExpress.持久.BaseImpl.v19.2.dll 程序集,该程序集位于 %PROGRAMFILES(x86)%_DevExpress 19.2_组件_Bin_Framework 路径或自定义程序集中。
在"选择类型"对话框中,选择要从程序集导入的持久类,然后单击"确定"。
These actions will import classes in read-only mode. Only persistent fields are displayed for these classes, and reference property types correspond to the underlying database types. However, you will be able to use the added classes as ancestors, and inherit new persistent classes from them in the Designer. To specify inheritance, select the Inheritance item in the Toolbox and draw a line from a descendant to a base class. Additionally, you will be able to use these classes as property types.
这些操作将以只读模式导入类。仅显示这些类的持久字段,引用属性类型对应于基础数据库类型。但是,您将能够将添加的类用作祖先,并在设计器中从它们继承新的持久类。要指定继承,请在工具箱中选择继承项,然后从后代到基类绘制一条线。此外,还可以将这些类用作属性类型。
Design the Task Object
设计任务对象
-
To add another business class, drag the XpObject item from the toolbox to the designer's surface. Focus the newly added object. In the Properties window, set the object's Name to Task.
-
要添加另一个业务类,请将 XpObject 项从工具箱拖动到设计器的曲面。聚焦新添加的对象。在"属性"窗口中,将对象的"名称"设置为"任务"。
-
Add the Subject and Description persistent properties of the string type using the approach described in the Design the Employee Object section.
-
使用"设计员工对象"部分中描述的方法添加字符串类型的"主题"和"描述"持久属性。
-
Next, see how to apply attributes to persistent properties. Focus the Description field. In the Properties window, expand the DBType category, and set Size to Unlimited.
- 接下来,了解如何将属性应用于持久属性。聚焦"描述"字段。在"属性"窗口中,展开 DBType 类别,并将"大小"设置为"无限制"。
As a result, the SizeAttribute will be applied to the Description property in the underlying code. The attribute's parameter will be set to SizeAttribute.Unlimited.
因此,Size属性将应用于基础代码中的"描述"属性。属性的参数将设置为 Size属性.无限制。
Add a One-to-Many Association
添加一对多关联
-
Add the AssignedTo persistent property of the Employee type to the Task class. This property will represent the "one" part of the one-to-many association.
-
将"已分配到员工"类型的持久属性添加到"任务"类。此属性将表示一对多关联的"一"部分。
-
In the Toolbox, focus the Association Object item. Draw a line from Employee to Task, to create the association.
-
在工具箱中,聚焦关联对象项。从"员工"到"任务"绘制一条线,以创建关联。
Note 注意
Alternatively, you can use the XpObject's title bar context menu to create an association. For details, refer to the Association section of the Data Model Designer topic.
或者,您可以使用 XpObject 的标题栏上下文菜单创建关联。有关详细信息,请参阅数据模型设计器主题的关联部分。
- Save the changes and close the designer.
- 保存更改并关闭设计器。
Add XAF-Specific Attributes in Code
在代码中添加特定于 XAF 的属性
-
In the Solution Explorer, expand the BusinessObjectsMySolutionDataModelCode folder. This folder contains the code generated by the designer. Open the Employee.cs (Employee.vb) file. Decorate the Employee class with the DefaultClassOptionsAttribute and ImageNameAttribute attributes to the Employee object. As a result, the Employee object will be added to the Navigation System, and an icon from the built-in library will be associated with this object.
-
在"解决方案资源管理器"中,展开"业务对象MySolutionDataModel 代码"文件夹。此文件夹包含设计器生成的代码。打开Employee.cs(员工.vb)文件。使用"默认类选项属性"和"图像名称属性"属性来装饰员工类。因此,Employer 对象将添加到导航系统中,并且内置库中的图标将与此对象关联。
using DevExpress.Persistent.Base; // ... [DefaultClassOptions, ImageName("BO_Employee")] public partial class Employee { public Employee(Session session) : base(session) { } public Employee() : base(Session.DefaultSession) { } public override void AfterConstruction() { base.AfterConstruction(); } }
-
Open the Task.cs (Task.vb) file. Decorate the Task class with the DefaultClassOptions and ImageName attributes.
-
打开Task.cs(Task.vb)文件。使用默认类选项和图像名称属性装饰任务类。
using DevExpress.Persistent.Base; // ... [DefaultClassOptions, ImageName("BO_Task")] public partial class Task { public Task(Session session) : base(session) { } public Task() : base(Session.DefaultSession) { } public override void AfterConstruction() { base.AfterConstruction(); } }
Note 注意
You can add more custom code to the auto-generated classes (e.g., add Action methods or override base class methods). Do not change the code located in files with the designer suffix - they contain designer-generated code, and should not be modified manually. The classes generated are declared as partial. Designed and custom class parts are located in different files.
您可以向自动生成的类添加更多自定义代码(例如,添加 Action 方法或重写基类方法)。不要更改位于具有设计器后缀的文件中的代码 - 它们包含设计器生成的代码,不应手动修改。生成的类声明为部分。设计和自定义类零件位于不同的文件中。
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.
或者,可以使用设计器应用属性。聚焦需要类或字段,并在"属性"窗口中指定"自定义属性"设置。
Run the Application
运行应用程序
Now you can run the Windows Forms and ASP.NET applications to see the result. These applications are completely based on the business model specified in the XPO Data Model Designer.
现在,您可以运行 Windows 窗体,ASP.NET应用程序以查看结果。这些应用程序完全基于 XPO 数据模型设计器中指定的业务模型。
-
Windows Forms
-
窗口窗体
-
ASP.NET
-
Mobile