Core Concepts > Basic Queries

Insert Statements

To start building INSERT queries, use the database accessor (static or dependency injected instance) followed by a Insert or InsertMany. An INSERT query relies on the data packages (entities) generated via the scaffolding process.

In addition to Execute, Insert and InsertMany include ExecuteAsync to asynchronously insert data.

Insert an Entity

In the example below, we set properties of the Person entity instance and pass it to the Insert method. dbExpression handles the mapping and parameterization of the data and sends it to the target database as a Merge statement. The Merge statement returns (as output) all the resulting columns, which are mapped back to the Person entity instance. This process accounts for the return of data created by your database platform:

  • columns with an identity specification
  • computed column values
  • columns with server side default constraints
var charlie = new Person()
{
    FirstName = "Charlie",
    LastName = "Brown",
    BirthDate = new DateTime(1950, 10, 2),
    GenderType = GenderType.Male,
    CreditLimit = 45000,
    YearOfLastCreditLimitReview = 2021,
    RegistrationDate = DateTimeOffset.Now,
    LastLoginDate = null,
};
// The person table relies on an identity specification 
// for the Id field (PK).  At this point, charlie.Id will be 0
// Person also contains a DateCreated column that is set via 
// a server side default binding of: GETDATE()

db.Insert(charlie).Into(dbo.Person).Execute();

// after the insert is executed, charlie.Id will contain 
// the identity generated on the database server
// charlie.DateCreated will contain the result of the 
// default constraint applied to the column

Insert a Batch of Entities

To insert a batch of records, use the InsertMany query type while building a query. InsertMany utilizes the same insert strategy as Insert described above. Upon return from execution of the InsertMany, all the supplied objects will have identity values, computed values and defaulted values.

var sally = new Person()
{
    FirstName = "Sally",
    LastName = "Brown",
    BirthDate = new DateTime(1959, 5, 26),
    GenderType = GenderType.Female,
    CreditLimit = 42000,
    YearOfLastCreditLimitReview = 2021,
    RegistrationDate = DateTimeOffset.Now,
    LastLoginDate = null,
};

var linus = new Person()
{
    FirstName = "Linus",
    LastName = "van Pelt",
    BirthDate = new DateTime(1952, 7, 14),
    GenderType = GenderType.Male,
    CreditLimit = 42000,
    YearOfLastCreditLimitReview = 2021,
    RegistrationDate = DateTimeOffset.Now,
    LastLoginDate = null,
};

var lucy = new Person()
{
    FirstName = "Lucy",
    LastName = "Van Pelt",
    BirthDate = new DateTime(1952, 3, 3),
    GenderType = GenderType.Female,
    CreditLimit = 42000,
    YearOfLastCreditLimitReview = 2021,
    RegistrationDate = DateTimeOffset.Now,
    LastLoginDate = null,
};

db.InsertMany(sally, linus, lucy).Into(dbo.Person).Execute();

// all properties based on identity column specifications, 
// default constraints or computed columns 
// will be populated on execution.
Previous
Select Statements

© 2024 dbExpression. All rights reserved.