Back to Devexpress

How to: Make a Class or Property Non-Persistent

xpo-2056-examples-how-to-make-a-class-or-property-non-persistent.md

latest1.6 KB
Original Source

How to: Make a Class or Property Non-Persistent

  • Aug 24, 2020

If you don’t want to store a property or a field that is persistent by default, you can explicitly mark it as non-persistent with the NonPersistentAttribute as shown in the following code example.

csharp
[NonPersistent]
    public string NonPersistentField;
vb
<NonPersistent()> _
    Public NonPersistentField As String

You can also make the entire class non-persistent. One common example is when you want to have your own base class (say, MyBusinessObject) for all persistent objects. By default, XPO will create a table MyBusinessObject where it will store a record for every business object in the system. To avoid this, mark MyBusinessObject as non-persistent as shown in the following code snippet.

csharp
[NonPersistent]
public class MyBusinessObject: XPObject {
}

public class Contact : MyBusinessObject {
}
vb
<NonPersistent()> _
Public Class MyBusinessObject : Inherits XPObject
End Class

Public Class Contact : Inherits MyBusinessObject
End Class

Note

XPO cannot restore non-persistent class references even if the actual object instance referenced is persistent. In the example above, XPO will not be able to restore reference to MyBusinessObject.

See Also

NonPersistentAttribute

PersistentAttribute