-
Notifications
You must be signed in to change notification settings - Fork 21
Default Values
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
}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
}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");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
}Copyright (c) 2018 Thong Nguyen (tumtumtum@gmail.com)