Generating Persistent Objects for Existing Data Tables
- 4 minutes to read
XPO supports the database-first development approach: you can generate persistent objects for your existing databases with no coding with a wizard. XPO installation ships with a design-time wizard that allows you to generate persistent objects for one or more data tables in your database at the same time.
Refer to the Basics of Creating Persistent Objects for Existing Data Tables topic for information on how to implement persistent classes manually.
The wizard does not support data tables whose primary keys are composed of multiple columns. These tables are neither displayed nor processed by the wizard.
Step by Step Instructions
Select the Add | New Item… command from the context menu in Solution Explorer.
Select the DevExpress ORM Data Model Wizard item in the invoked dialog window, specify the data model name (for example, MyDataModel) and click Add.
In the invoked Data Model Wizard, select Map to an existing database and click Next. Specify connection settings to the database containing a target data table(s):
Wizard supports various database systems (Microsoft SQL Server, DB2, MySql, Firebird, etc.). In the Provider combo box, select a required database type. Note that the corresponding database provider assembly must be available on your machine, otherwise the wizard stops working.
Click Next after connection settings have been specified.
When the Wizard initializes the ADO.NET provider and establishes a database connection, it may display the following error messages: “Could not find assembly …“, “None of the following assemblies found: …“, “Could not load assembly …“, “Could not find type …“, and others. In this case, ensure that you have the required database provider installed, and that database provider assemblies are available. Assemblies required by XPO data store adapters are listed in the Database Systems Supported by XPO topic.
The Wizard requires assemblies built against the .NET Framework to work.
Place the required DLLs in a location where the Visual Studio process can find them. You can install .NET assemblies to the Global Assembly Cache (GAC) and copy the required native DLLs (if any) to %SystemRoot%\System32, to make them available globally. For details, see FAQ: ORM Data Model Designer and Wizard.
In the next step, the wizard displays a list of tables that can be mapped to persistent objects.
Select a table(or tables) to be mapped to a persistent object (or to persistant objects). For each table, select columns that should be mapped to the persistent object’s properties. For unchecked columns, persistent properties are not generated.
The names of persistent classes and their properties match the names of selected tables and their columns. You can add/remove custom prefixes and suffixes, and change the case style of names (for example, capitalize names). The wizard displays the resulting names. You can edit them later in the Data Model Designer. The classes and properties with modified names are mapped to the corresponding tables and columns using the PersistentAttribute attribute.
Click Next, and then click Finish to close the wizard. Click the File | Save All menu item in the Visual Studio toolbar to generate persistent classes. The generated data model will be opened in the Data Model Designer.
If you do not want to use the Data Model Designer and prefer to edit generated classes manually, do the following:
- In the wizard, uncheck the Create partial declarations for manual editing option. As a result, the wizard generates a single code file for each class without partial declarations.
- After the wizard is completed, remove the generated MyDataModel.xpo file (here, “MyDataModel” is the name you specified in step 2 above).
- Use classes in the MyDataModelCode folder as regular Code First classes - make required modifications in code manually.
You can try the functionality described here in the Querying a Data Store | Generating Persistent Classes for an Existing Database section of the XPO Tutorials demo (C:\Users\Public\Documents\DevExpress Demos 21.1\Components\WinForms\Bin\XpoTutorials.exe).