[Expand]General Information
[Expand]WinForms Controls
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]ASP.NET Core Bootstrap Controls
[Expand]WPF Controls
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Office File API
[Expand]Reporting
[Expand]Report and Dashboard Server
[Expand]Dashboard
[Expand]eXpressApp Framework
[Collapse]eXpress Persistent Objects
 [Expand]Product Information
 [Expand]Getting Started
 [Expand]Fundamentals
 [Expand]Feature Center
 [Collapse]Concepts
   Basics of Creating Persistent Objects for Existing Data Tables
   Creating a Persistent Object
   Creating a Session
   Creating an XPCollection
   Custom Collection Types
   Deferred and Immediate Object Deletion
   Explicit Units of Work
   Filtering Basics
   Generating Persistent Objects for Existing Data Tables
   How to: Add Persistence to an Existing Hierarchy by Changing the Base Inheritance
   How to: Add Persistence to an Existing Hierarchy by Using Session-less Persistent Objects
   How to: Connect to a Data Store
   Inheritance Mapping
   Nested Units of Work
   NULL Value Handling Specifics
   Optimistic Concurrency Control
   Pageable Collections
   Property Descriptors
   Relationships Between Objects
   Saving Persistent Objects
   Simplified Criteria Syntax
   Simplified Property Syntax
   Sorting Basics
   Using Explicit Transactions
   Using Transactions
   Value Converters
   Working with Sessions
   XPCollection Concepts
   XPDataView Concepts
   XPO Classes Comparison
   XPView Concepts
 [Expand]Design-Time Features
 [Expand]Examples
 [Expand]Member Tables
 [Expand]API Reference
[Expand]CodeRush
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation
View this topic on docs.devexpress.com (Learn more)

Creating a Persistent Object

The easiest way to define a persistent object is to inherit from the XPObject class. Every persistent object includes the Oid (object identifier) property, which uniquely identifies the object. The XPObject class implements automatic key generation. An auto-generated integer key is mapped to the 'OID' field. Key auto-generation is supported for System.Int32, System.Int64 and System.Guid data types.

Expanded Example - Declaring a persistent object

You can use the DevExpress v19.1 ORM Persistent Object project item template to create a persistent class.

The key properties or fields can also be specified manually. In this instance, you need to inherit your class from the XPCustomObject class. Use the KeyAttribute to mark a property(s) or field(s) as the key. The attribute's KeyAttribute.AutoGenerate property specifies whether the key is generated automatically.

By default, XPO creates a MS Access compatible database in the same directory as the one in which the executable of your application resides. By specifying the default connection, you can easily change this (see How to: Connect to a Data Store). In either case, XPO automatically creates the necessary database structure for you.

The code below creates a Contact table and inserts one record into it.

Expanded Example - Creating a Contact record

Expanded Where to Place the Initialization Code for a Persistent Object

Initialization in this section means setting default values for a new XPObject's properties. Later these properties can be modified and saved into a persistent storage (a physical database). When objects are restored from the persistent storage, property values must not be overridden with default values.

To do this, you should override the XPObject's PersistentBase.AfterConstruction method.

The following sample code shows how to override the PersistentBase.AfterConstruction method to set the afterconstructcalled field's value immediately after the TestConstruct object has been initialized.

Expanded See Also

Is this topic helpful?​​​​​​​