How to: How to Generate XPO Business Classes for Existing Data Tables: generating XPO existing traffic class data table

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.

Through tutorials and other documentation sources, you learned how to create a business class for XAF application. If you have a business class applications, the database application has a database table. However, the reality is that most programmers than starting from scratch to build a new application, but to preserve the existing database. In this case, they can use XPO Data Model Wizard generates business model declared as specified in the old database. Generating a traffic class of an existing database to be used in applications XAF following steps.

 

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

If you prefer to watch video rather than exercise these step by step instructions, you can access the appropriate tutorial on our YouTube channel: XAF: Creating an application based on an existing database

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 model generated data

  • XAF to create new solutions using DevExpress v19.2 XAF Solution Wizard template.

  • Right-click the module project "business objects" folder. Select "Add |. In the new project called" Add New Project "dialog box, select situated" DevExpress ORM Data Model Wizard template developers express "category in the name of the new project is set to MySolutionDataModel.xpo, and then click. "Add." You will see MySolutionDataModel.xpo items have been added, and call the Wizard dialog box.

  • In the Wizard dialog box call, select "mapped to an existing database," and then click "Next."

    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.

  • Assigned to the connection settings data contains the target database. Wizard supports multiple database systems (MS SQL Server, DB2, MySql, Firebird, etc.). Provider using a combo box to select the desired type of database. Please note that your computer must be on the Global Assembly Cache (GAC) registered in the corresponding database provider assemblies, otherwise the wizard will fail. In this example, we will use the "North Wind Trader" demo database. By default, this database is supplied with of DXperience, and installed in the% PUBLIC% _ presentation document _DevExpress 19.2 [component] in nwind.mdb data.

 

XpoDesigner_LegacyDB_Wizard2

Click Next after connection settings have been specified.

After you specify the connection settings, click "Next."

  • 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

Run the application

Now, you can run WinForms, ASP.NET and mobile applications to see the results. These applications are based entirely on the business model for the generation of the old database.

  • WinForms
  • ASP.NET
  • mobile

WinForms

 

Guess you like

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