Back to Devexpress

NullableAttribute Class

xpo-devexpress-dot-xpo-60efc819.md

latest3.6 KB
Original Source

NullableAttribute Class

Applies to persistent class’ fields or properties. Specifies if a nullable column should be created when updating the database schema for the target property/field.

Namespace : DevExpress.Xpo

Assembly : DevExpress.Xpo.v25.2.dll

NuGet Package : DevExpress.Xpo

Declaration

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

Remarks

To change the NullableAttribute value for the entire persistent class, apply the NullableBehaviorAttribute instead. To change this setting globally, use the static XpoDefault.NullableBehavior property.

The NullableAttribute does not apply to persistent objects’ reference type properties. XPO uses these properties as foreign keys and creates NULL columns for this type. You cannot change this behavior.

See Nullable Behavior and Nullable Columns for more information.

The code below demonstrates how to apply the NullableAttribute. The example generates a database table that allows null in the Age column and does not allow null in the Name column.

csharp
using DevExpress.Xpo;

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

// ...
class Person : XPObject {
    public Person(Session session) : base(session) { }
    string fName;
    int fAge;

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

    [Nullable(true)]
    public int Age {
        get {
            return fAge;
        }
        set {
            SetPropertyValue(nameof(fName), ref fAge, value);
        }
    }
}
vb
Imports DevExpress.Xpo

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

' ...
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

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

    <Nullable(True)>
    Public Property Age() As Integer
        Get
            Return fAge
        End Get
        Set(ByVal value As Integer)
            SetPropertyValue(NameOf(fAge), fAge, value)
        End Set
    End Property
End Class

Inheritance

Object Attribute NullableAttribute

See Also

NullableAttribute Members

DevExpress.Xpo Namespace