Knight Data Access Layer
Default Values
Basic Tasks C# > Mapping > Default Values

Default values allow you to fill in defaults before it is saved to the database.

 

//We have a DefaultValue attribute on the Person class that fills in the default value

//Example:

// [DefaultValue(1)]

// public int PersonTypeId { get; set; }

 

//Create a new Person to save to the database

Person currentPerson = new Person();

currentPerson.FirstName = "John";

currentPerson.LastName = "Smith";

 

//Save it to the database (see the SQL in the console)

_db.Save(currentPerson);

 

//This will be equal to 1

Console.WriteLine(currentPerson.PersonTypeId);