Click or drag to resize
SqlExpressionEGetInsertSql Method
Creates a INSERT SQL based on the passed parameters.

Namespace: LiteRepository
Assembly: LiteRepository (in LiteRepository.dll) Version: 2.0.4
Syntax
C#
public string GetInsertSql(
	Type type = null
)

Parameters

type (Optional)
Type: SystemType
Type that contains subset of E members. Used for generate fields and values lists.

Return Value

Type: String
A string with a insert query.
Examples

Suppose we have a class:

C#
public class User {
    [SqlKey]
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string SecondName { get; set; }
    public DateTime Birthday { get; set; }
    [SqlIgnore]
    public string FullName { get { return FirstName + " " + SecondName; } }
}

If you don't passed any parameter into method, it returns default insert SQL:

C#
var sql = GetInsertSql();
// insert into User (Id, FirstName, SecondName, Birthday) values (@Id, @FirstName, @SecondName, @Birthday);

Passing type you sets subset of columns which you want to insert:

C#
var p = new { Id = 0, FirstName = "", SecondName = "" };
var sql = GetInsertSql(type:p.GetType()); 
// insert into User (Id, FirstName, SecondName) values (@Id, @FirstName, @SecondName);

If E is identity entity, then method returns special SQL:

insert into User (FirstName, SecondName, Birthday) values (@FirstName, @SecondName, @Birthday);
select scope_identity();
See Also