Back to Devexpress

Generate XPO Business Classes for Existing Data Tables

expressappframework-113451-business-model-design-orm-business-model-design-with-xpo-generate-xpo-business-classes-for-existing-data-tables.md

latest5.8 KB
Original Source

Generate XPO Business Classes for Existing Data Tables

  • Dec 29, 2025
  • 5 minutes to read

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.

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.

Generate an XPO Data Model

  • Create a new XAF solution using the DevExpress Template Kit.

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

  • Specify connection settings to the database containing the target data. The wizard supports multiple database systems (Microsoft SQL Server, DB2, MySql, Firebird, and so on). 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 25.2\Components\Data\nwind.mdb, by default.

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

  • In the Visual Studio toolbar, click save. The generated code files will appear in the Solution Explorer , in the BusinessObjects\MySolutionDataModelCode folder.

Add XAF-Specific Attributes in Code

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

  • Open the Order.cs (Order.vb) file. Decorate the Order class with the DefaultClassOptions and ImageName attributes.

You can add more custom code to the auto-generated classes (for example, 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

We recommend that you use the System.ComponentModel.DefaultProperty attribute to specify the default property for each generated class Default property is treated as a human-readable identifier in an XAF application 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

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.

Run the Application

Now you can run the application to see the result. The application is completely based on the business model generated for the legacy database.

See Also

Map a Persistent Class to a Database View Which Has No Key Field