Default Values

Thong Nguyen edited this page Mar 28, 2017 · 6 revisions

Default Values

Nullable types

Types all follow C# CLR conventions in that value types are converted to NOT NULL column types and reference types remain SQL nullable.

[DataAccessObject]
public class Car : DataAccessObject<Guid>
{
    [PersistedMember]
    public virtual int EngineLitres { get; set; } // COLUMN NOT NULL

    [PersistedMember]
    public virtual DateTime RegistrationDate { get; set; } // COLUMN NOT NULL

    [PersistedMember]
    public virtual string Name { get; set; } // COLUMN NULL
}

Declaring Default Values

You can declare default values for properties using the Platform.Validation.DefaultValueAttribute. Properties with default values are converted to SQL columns with DEFAULT VALUE constraints.

[DataAccessObject]
public class Car : DataAccessObject<Guid>
{
    [PersistedMember, DefaultValue(2)]
    public virtual int EngineLitres { get; set; } // COLUMN NOT NULL

    [PersistedMember, DefaultValue("2016-12-31 00:00")]
    public virtual DateTime RegistrationDate { get; set; } // COLUMN NOT NULL

    [PersistedMember]
    public virtual string Name { get; set; } // COLUMN NULL
}

Partial INSERTS

Shaolinq prefers to send the minimum amount of data to and from the database. This means properties that are not explicitly set will not be submitted to the server. New objects with NOT NULL (value type or ValueRequired = true) properties must be explicitly set before the object is committed to the database. Failing to do so will result in a MissingPropertyValueException originating from there database because the INSERT statement generated by Shaolinq will not include those columns.

using (var scope = new DataAccessScope())
{
    var car = model.Cars.Create();
    car.Name = "Harrison's 2017 5-series"

    scope.Complete();
}

Resulting SQL will not include EngineLitres or RegistrationDate:

INSERT INTO Car(Name) VALUES ("Harrison's 2017 5-series");

Nullable value type properties

Inbuilt nullable types will automatically map to SQL nullable columns (no NOT NULL constraint).

[DataAccessObject]
public class Car : DataAccessObject<Guid>
{
    [PersistedMember]
    public virtual int? EngineLitres { get; set; } // COLUMN NULL
}
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.