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 项已添加,并调用向导对话框。

  • 在调用的向导对话框中,选择"映射到现有数据库",然后单击"下一步"。

    XpoDesigner_LegacyDB_Wizard1

  • 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 中。

XpoDesigner_LegacyDB_Wizard2

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.

  • 在下一步中,向导显示可映射到持久类的表的列表。选择要映射到持久对象的表,为每个表选择将映射到持久对象属性的列。对于未选中的列,将不会生成持久属性。例如,选择"客户"和"订单"表。

    XpoDesigner_LegacyDB_Wizard3

  • Click Next to close the wizard. The generated data model will be shown in the XPO Data Model designer.

  • 单击"下一步"关闭向导。生成的数据模型将显示在 XPO 数据模型设计器中。

XpoDesigner_LegacyDB_Designer

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 代码文件夹中。

    XpoDesigner_LegacyDB_SolutionExplorer

    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.

或者,可以使用设计器应用属性。聚焦需要类或字段,并在"属性"窗口中指定"自定义属性"设置。

CustomAttributes

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
  • 移动

WinForms

 

猜你喜欢

转载自www.cnblogs.com/foreachlife/p/How-to-Generate-XPO-Business-Classes-for-Existing-Data-Tables.html