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 业务类的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. XAF-从业务类继承 (XPO)

    In this lesson, you will learn how to implement business classes for your application using the Busi ...

  5. Data Being Added Conflicts with Existing Data

    While developing a page with multiple scrolls levels, and especially when using a grid, you may get ...

  6. 17.1.1.8?Setting Up Replication with Existing Data设置复制使用存在的数据

    17.1.1.8?Setting Up Replication with Existing Data设置复制使用存在的数据 当设置复制使用存在的数据,你需要确定如何最好的从master 得到数据到sl ...

  7. 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 ...

  8. numpy 基于现有数据创建ndarray(from existing data)

    1 numpy.array array(object[, dtype=None, copy=True, order='K', subok=False, ndmin=0]) 2 numpy.asarra ...

  9. TMF大数据分析指南 Unleashing Business Value in Big Data(一)

    大数据分析指南 TMF Frameworx最佳实践 Unleashing Business Value in Big Data 前言 此文节选自TMF Big Data Analytics Guide ...

随机推荐

  1. #华为云·寻找黑马程序员#【代码重构之路】如何“消除”if/else

    1. 背景 if/else是高级编程语言中最基础的功能,虽然 if/else 是必须的,但滥用 if/else,特别是各种大量的if/else嵌套,会对代码的可读性.可维护性造成很大伤害,对于阅读代码 ...

  2. 你知道,HTTPS用的是对称加密还是非对称加密?

    1.引言 随着互联网安全意识的普遍提高,对安全要求稍高的应用中,HTTPS的使用是很常见的,甚至在1年前,苹果公司就将使用HTTPS作为APP上架苹果应用市场的先决条件之一(详见<苹果即将强制实 ...

  3. list tuple dict 方法

    >>> dir(list)['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir_ ...

  4. Spring Data JPA 的一对多,一对多,多对多操作

    一对一的关联关系   需求:用户与角色的一对一的关联关系 用户:一方 角色:一方     创建 Users 实体 @Entity @Table(name = "t_users") ...

  5. Spring简介即Spring Ioc

    Spring框架简介 Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE De ...

  6. 小程序 - 简单实现mixin功能

    前言 在业务中有没有一个场景:多个页面需要用到一样的 data 和 method,或者生命周期都需要执行同样的操作.我们在每个页面都写上重复的代码,一但功能修改就要更新多个页面,在后期维护起来会很麻烦 ...

  7. WebAPI之postman变量和session/token

    postman使用变量: 之前我们请求里面的主机地址都是localhost,表示本机.而在实际测试过程中,服务器地址往往并非是在本机上的,这时候就需要用到变量. postman支持多个测试环境,一个环 ...

  8. linux—netstat

    netstat--option -a: 列出所有端口,监听的没有监听的     -t: 显示tcp相关的选项 -u: 显示udp相关的选项 -l: 仅仅显示监听选项 -p:  显示与连接有关的程序名和 ...

  9. Windows基础

    目录 一. 硬件概述 二.常见硬件设备 三.文件系统 四. 文件类型和DOS命令 五 .批处理 六 .TCP/IP概述和Windows配置 七.网络设置.DNS.邮件★ 一. 硬件概述 1.计算机的发 ...

  10. 为什么要使用MQ消息中间件?这3个点让你彻底明白!

    前言 一个用消息队列的人,不知道为啥用,有点尴尬.没有复习这点,很容易被问蒙,然后就开始胡扯了. 回答:这个问题,咱只答三个最主要的应用场景,不可否认还有其他的,但是只答三个主要的,即以下六个字: 解 ...