Back to Devexpress

NullableBehaviorAttribute Class

xpo-devexpress-dot-xpo-7f261785.md

latest6.4 KB
Original Source

NullableBehaviorAttribute Class

Applied to persistent classes. Specifies if nullable columns should be created when updating the database schema for the target class.

Namespace : DevExpress.Xpo

Assembly : DevExpress.Xpo.v25.2.dll

NuGet Package : DevExpress.Xpo

Declaration

csharp
[AttributeUsage(AttributeTargets.Class, Inherited = true)]
public sealed class NullableBehaviorAttribute :
    Attribute
vb
<AttributeUsage(AttributeTargets.Class, Inherited:=True)>
Public NotInheritable Class NullableBehaviorAttribute
    Inherits Attribute

Remarks

Apply the NullableBehaviorAttribute to your persistent class declaration and pass one of the NullableBehavior enumeration values to the attribute.

You can change the behavior globally. Use the static XpoDefault.NullableBehavior property. To change the behavior for a specific field of property, use the NullableAttribute.

See Nullable Behavior and Nullable Columns for more information.

Examples

The examples below demonstrates how to apply the NullableBehaviorAttribute.

Example 1

The code below uses the ByUnderlyingType enum value. The example generates a database table that allows null in the Name and BirthDate column and does not allow null in the Age column.

csharp
using DevExpress.Xpo;

// ...
using(UnitOfWork unitOfWork = new UnitOfWork(dataLayer)) {
    unitOfWork.UpdateSchema(typeof(Person));
}

// ...
[NullableBehavior(NullableBehavior.ByUnderlyingType)]
class Person : XPObject {
    public Person(Session session) : base(session) { }
    string fName;
    int fAge;
    DateTime? fBirthDate;

    public string Name {
        get {
            return fName;
        }
        set {
            SetPropertyValue(nameof(fName), ref fName, value);
        }
    }

    public int Age {
        get {
            return fAge;
        }
        set {
            SetPropertyValue(nameof(fName), ref fAge, value);
        }
    }

    public DateTime? BirthDate {
        get {
            return fBirthDate;
        }
        set {
            SetPropertyValue(nameof(fName), ref fBirthDate, value);
        }
    }
}
vb
Imports DevExpress.Xpo

' ...
Using unitOfWork As New UnitOfWork(dataLayer)
    unitOfWork.UpdateSchema(GetType(Person))
End Using

' ...
<NullableBehavior(NullableBehavior.ByUnderlyingType)>
Friend Class Person
    Inherits XPObject

    Public Sub New(ByVal session As Session)
        MyBase.New(session)
    End Sub
    Private fName As String
    Private fAge As Integer
    Private fBirthDate? As Date

    Public Property Name() As String
        Get
            Return fName
        End Get
        Set(ByVal value As String)
            SetPropertyValue(NameOf(fName), fName, value)
        End Set
    End Property

    Public Property Age() As Integer
        Get
            Return fAge
        End Get
        Set(ByVal value As Integer)
            SetPropertyValue(NameOf(fName), fAge, value)
        End Set
    End Property

    Public Property BirthDate() As Date?
        Get
            Return fBirthDate
        End Get
        Set(ByVal value? As Date)
            SetPropertyValue(NameOf(fName), fBirthDate, value)
        End Set
    End Property
End Class

Example 2

The code below uses the AlwaysAllowNulls enum value. The example generates a database table that allows null in all columns.

csharp
using DevExpress.Xpo;

// ...
using(UnitOfWork unitOfWork = new UnitOfWork(dataLayer)) {
    unitOfWork.UpdateSchema(typeof(Person));
}

// ...
[NullableBehavior(NullableBehavior.AlwaysAllowNulls)]
class Person : XPObject {
    public Person(Session session) : base(session) { }
    string fName;
    int fAge;
    DateTime? fBirthDate;

    public string Name {
        get {
            return fName;
        }
        set {
            SetPropertyValue(nameof(fName), ref fName, value);
        }
    }

    public int Age {
        get {
            return fAge;
        }
        set {
            SetPropertyValue(nameof(fName), ref fAge, value);
        }
    }

    public DateTime? BirthDate {
        get {
            return fBirthDate;
        }
        set {
            SetPropertyValue(nameof(fName), ref fBirthDate, value);
        }
    }
}
vb
Imports DevExpress.Xpo

' ...
Using unitOfWork As New UnitOfWork(dataLayer)
    unitOfWork.UpdateSchema(GetType(Person))
End Using

' ...
<NullableBehavior(NullableBehavior.AlwaysAllowNulls)>
Friend Class Person
    Inherits XPObject

    Public Sub New(ByVal session As Session)
        MyBase.New(session)
    End Sub
    Private fName As String
    Private fAge As Integer
    Private fBirthDate? As Date

    Public Property Name() As String
        Get
            Return fName
        End Get
        Set(ByVal value As String)
            SetPropertyValue(NameOf(fName), fName, value)
        End Set
    End Property

    Public Property Age() As Integer
        Get
            Return fAge
        End Get
        Set(ByVal value As Integer)
            SetPropertyValue(NameOf(fName), fAge, value)
        End Set
    End Property

    Public Property BirthDate() As Date?
        Get
            Return fBirthDate
        End Get
        Set(ByVal value? As Date)
            SetPropertyValue(NameOf(fName), fBirthDate, value)
        End Set
    End Property
End Class

Inheritance

Object Attribute NullableBehaviorAttribute

See Also

NullableBehaviorAttribute Members

Data Types Supported by XPO

DevExpress.Xpo Namespace