Back to Devexpress

Map Persistent Objects to Database Views

xpo-403909-create-a-data-model-map-persistent-objects-to-database-views.md

latest2.9 KB
Original Source

Map Persistent Objects to Database Views

  • May 13, 2022
  • 2 minutes to read

Create a Persistent Class

Create a persistent class. The class name should match the view name.

Assign the Persistent attribute to the persistent class if its name differs from the view name (see the example below).

csharp
[Persistent("DatabaseViewName")]
public class MyView : XPLiteObject {  

}
vb
<Persistent("DatabaseViewName")> _  
Public Class MyView  
    Inherits XPLiteObject

End Class

Important

The Persistent attribute is case sensitive.

Create a Primary Key

To work correctly with an existing database view, the view must have a field with unique values (the key field). Create a persistent property with the same name as the view’s key field, and assign the Key attribute to it.

If the view does not have a field with unique values, create a composite key as shown in the following example.

csharp
public struct MyViewKey {  
    [Persistent("FirstColumn")]  
    public string FirstColumn;  
    [Persistent("SecondColumn")]  
    public string SecondColumn; 
} 

[Persistent("DatabaseViewName")]
public class MyView : XPLiteObject {  
    [Key, Persistent]  
    public MyViewKey Key;  

    [PersistentAlias("Key.FirstColumn")]  
    public string FirstValue { get { return Key.FirstColumn; } }  
    [PersistentAlias("Key.SecondColumn")]  
    public string SecondValue { get { return Key.SecondColumn; } }  
}
vb
Public Structure MyViewKey  
    <Persistent("FirstColumn")> _  
    Public FirstColumn As String  
    <Persistent("SecondColumn")> _  
    Public SecondColumn As String  
End Structure  

<Persistent("DatabaseViewName")> _  
Public Class MyView  
    Inherits XPLiteObject
    <Key, Persistent> _  
    Public Key As MyViewKey  

    <PersistentAlias("Key.FirstColumn")> _  
    Public ReadOnly Property FirstValue() As String  
        Get  
            Return Key.FirstColumn  
        End Get  
    End Property  
    <PersistentAlias("Key.SecondColumn")> _  
    Public ReadOnly Property SecondValue() As String  
        Get  
            Return Key.SecondColumn  
        End Get  
    End Property
End Class

Create Persistent Properties

Create public persistent properties for all data fields or specific fields. The type of persistent properties must match the type of the corresponding fields in the database view.

See Also

Basics of Creating Persistent Objects for Existing Data Tables

Create a Persistent Object