Skip to main content
A newer version of this page is available. .
.NET Framework 4.5.2+

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.v20.2.dll

NuGet Package: DevExpress.Xpo

Declaration

[AttributeUsage(AttributeTargets.Class, Inherited = true)]
public sealed class NullableBehaviorAttribute :
    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.

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);
        }
    }
}

Example 2

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

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);
        }
    }
}

Inheritance

Object
Attribute
NullableBehaviorAttribute
See Also