| SqlExpressionEGetInsertSql Method |
Namespace: LiteRepository
public string GetInsertSql( Type type = null )
Suppose we have a class:
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:
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:
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();