学习011-03-08 Generate XPO Business Classes for Existing Data Tables(为现有数据表生成 XPO 业务类)

Generate XPO Business Classes for Existing Data Tables(为现有数据表生成 XPO 业务类)

From 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(生成XPO数据模型)

  • Create a new XAF solution using the DevExpress v24.1 XAF Template Gallery template.
    使用DevExpress v24.1 XAF模板库模板创建新的XAF解决方案。

  • 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.
    右键单击位于模块项目中的BusinessObjects文件夹。选择添加|新项目。在调用的添加新项目对话框中,选择位于DevExpress类别中的DevExpress ORM数据模型向导模板。将新项目的名称设置为MySolutionDataModel. xpo,然后单击添加。您将看到添加了MySolutionDataModel.xpo项目并调用了向导对话框。

  • In the invoked wizard dialog, choose Map to an existing database and click Next.
    在调用的向导对话框中,选择映射到现有数据库,然后单击下一步。
    在这里插入图片描述

  • Specify connection settings to the database containing the target data. Multiple database systems (Microsoft 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 24.1 \Components\Data\nwind.mdb, by default.
    指定与包含目标数据的数据库的连接设置。向导支持多个数据库系统(MicrosoftSQLServer、DB2、MySql、Firebird等)。使用Provider组合框选择所需的数据库类型。请注意,相应的数据库提供程序集必须在您机器上的全局程序集缓存(GAC)中注册,否则向导将失败。在本例中,我们将使用“NorthWind Traders”演示数据库。该数据库随DXperience一起提供,默认安装在%PUBLIC%\Documents\DevExpress Demos 24.1\Components\Data\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工具栏中,单击保存。生成的代码文件将出现在解决方案资源管理器的BusinessObjects\MySolutionDataModelCode文件夹中。
    在这里插入图片描述

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 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.
打开Customor. cs文件。使用DefaultClassOptionsAttribute和ImageNameAtual属性装饰Customer类。因此,将向导航系统添加Customer对象,并且将与该对象关联来自内置库的图标。

C#
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 file. Decorate the Order class with the DefaultClassOptions and ImageName attributes.
打开Order. cs文件。使用DefaultClassOptions和ImageName属性装饰Order类。

C#
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. ComponentModel.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=(localdb)\mssqllocaldb;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 MySolution.Module\app.config file.
使用默认连接字符串生成空XAF应用程序: Data Source=(localdb)\mssqllocaldb;Initial Catalog=MyApplication;Integrated Security=SSPI;Pooling=false.您应该更改它,以便应用程序使用所需的数据库。更改应用程序配置文件中的连接字符串。您可以从自动创建的MySolutions. Module\app.config文件中复制有效的连接字符串。

Run the Application(运行应用程序)

Now you can run the WinForms and ASP.NET Web Forms applications to see the result. These applications are completely based on the business model generated for the legacy database.
现在您可以运行WinForms和ASP.NET Web Forms应用程序来查看结果,这些应用程序完全基于为遗留数据库生成的业务模型。

WinForms
在这里插入图片描述

ASP.NET Web Forms
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

汤姆•猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值