Knight Data Access Layer
Loading By Primary Key
Basic Tasks C# > Loading Data > Loading By Primary Key

Loading By Primary Key

To load by primary key, a unique single primary key must be defined on the table.  Primary keys are automatically mapped.  See Mapping Primary Keys for more information.

 

//Create a new Person to save to the database

Person person = new Person();

person.FirstName = "John";

person.LastName = "Smith";

person.DateCreated = DateTime.Now;

 

//Get the current instance of the helper

IDataHelper db = DataHelper.SessionFactory();

 

//Save it

db.Save(person);

 

//Load by primary key

Person personCopy = db.LoadByPrimaryKey<Person>(person.PersonId);