Skip to main content

OrmLiteWriteApi

Assembly: ServiceStack.OrmLite.dll
View Source
Declaration
public static class OrmLiteWriteApi : object

Methods

GetLastSql(IDbConnection)

Get the last SQL statement that was executed.

View Source
Declaration
public static string GetLastSql(this IDbConnection dbConn)
Returns

System.String

Parameters
TypeName
IDbConnectiondbConn

GetLastSqlAndParams(IDbCommand)

View Source
Declaration
public static string GetLastSqlAndParams(this IDbCommand dbCmd)
Returns

System.String

Parameters
TypeName
IDbCommanddbCmd

ExecuteSql(IDbConnection, String)

Execute any arbitrary raw SQL.

View Source
Declaration
public static int ExecuteSql(this IDbConnection dbConn, string sql)
Returns

System.Int32: number of rows affected

Parameters
TypeName
IDbConnectiondbConn
System.Stringsql

ExecuteSql(IDbConnection, String, Object)

Execute any arbitrary raw SQL with db params.

View Source
Declaration
public static int ExecuteSql(this IDbConnection dbConn, string sql, object dbParams)
Returns

System.Int32: number of rows affected

Parameters
TypeName
IDbConnectiondbConn
System.Stringsql
System.ObjectdbParams

ExecuteSql(IDbConnection, String, Dictionary<String, Object>)

Execute any arbitrary raw SQL with db params.

View Source
Declaration
public static int ExecuteSql(this IDbConnection dbConn, string sql, Dictionary<string, object> dbParams)
Returns

System.Int32: number of rows affected

Parameters
TypeName
IDbConnectiondbConn
System.Stringsql
Dictionary<System.String,System.Object>dbParams

Insert<T>(IDbConnection, T, Boolean, Boolean)

Insert 1 POCO, use selectIdentity to retrieve the last insert AutoIncrement id (if any). E.g: <p>var id = db.Insert(new Person { Id = 1, FirstName = "Jimi }, selectIdentity:true)</p>

View Source
Declaration
public static long Insert<T>(this IDbConnection dbConn, T obj, bool selectIdentity = false, bool enableIdentityInsert = false)
Returns

System.Int64

Parameters
TypeName
IDbConnectiondbConn
<T>obj
System.BooleanselectIdentity
System.BooleanenableIdentityInsert
Type Parameters
  • T

Insert<T>(IDbConnection, T, Action<IDbCommand>, Boolean)

Insert 1 POCO and modify populated IDbCommand with a commandFilter. E.g: <p>var id = db.Insert(new Person { Id = 1, FirstName = "Jimi }, dbCmd => applyFilter(dbCmd))</p>

View Source
Declaration
public static long Insert<T>(this IDbConnection dbConn, T obj, Action<IDbCommand> commandFilter, bool selectIdentity = false)
Returns

System.Int64

Parameters
TypeName
IDbConnectiondbConn
<T>obj
Action<IDbCommand>commandFilter
System.BooleanselectIdentity
Type Parameters
  • T

Insert<T>(IDbConnection, Dictionary<String, Object>, Boolean)

Insert 1 POCO, use selectIdentity to retrieve the last insert AutoIncrement id (if any). E.g: <p>var id = db.Insert(new Dictionary<string,object> { ["Id"] = 1, ["FirstName"] = "Jimi }, selectIdentity:true)</p>

View Source
Declaration
public static long Insert<T>(this IDbConnection dbConn, Dictionary<string, object> obj, bool selectIdentity = false)
Returns

System.Int64

Parameters
TypeName
IDbConnectiondbConn
Dictionary<System.String,System.Object>obj
System.BooleanselectIdentity
Type Parameters
  • T

Insert<T>(IDbConnection, Action<IDbCommand>, Dictionary<String, Object>, Boolean)

Insert 1 POCO, use selectIdentity to retrieve the last insert AutoIncrement id (if any). E.g: <p>var id = db.Insert(new Dictionary<string,object> { ["Id"] = 1, ["FirstName"] = "Jimi }, dbCmd => applyFilter(dbCmd))</p>

View Source
Declaration
public static long Insert<T>(this IDbConnection dbConn, Action<IDbCommand> commandFilter, Dictionary<string, object> obj, bool selectIdentity = false)
Returns

System.Int64

Parameters
TypeName
IDbConnectiondbConn
Action<IDbCommand>commandFilter
Dictionary<System.String,System.Object>obj
System.BooleanselectIdentity
Type Parameters
  • T

InsertUsingDefaults<T>(IDbConnection, T[])

Insert 1 or more POCOs in a transaction using Table default values when defined. E.g: <p>db.InsertUsingDefaults(new Person { FirstName = "Tupac", LastName = "Shakur" },</p> <p> new Person { FirstName = "Biggie", LastName = "Smalls" })</p>

View Source
Declaration
public static void InsertUsingDefaults<T>(this IDbConnection dbConn, params T[] objs)
Parameters
TypeName
IDbConnectiondbConn
<T>[]objs
Type Parameters
  • T

InsertIntoSelect<T>(IDbConnection, ISqlExpression)

Insert results from SELECT SqlExpression, use selectIdentity to retrieve the last insert AutoIncrement id (if any). E.g: <p>db.InsertIntoSelect<Contact>(db.From<Person>().Select(x => new { x.Id, Surname == x.LastName }))</p>

View Source
Declaration
public static long InsertIntoSelect<T>(this IDbConnection dbConn, ISqlExpression query)
Returns

System.Int64

Parameters
TypeName
IDbConnectiondbConn
ServiceStack.OrmLite.ISqlExpressionquery
Type Parameters
  • T

InsertIntoSelect<T>(IDbConnection, ISqlExpression, Action<IDbCommand>)

Insert results from SELECT SqlExpression, use selectIdentity to retrieve the last insert AutoIncrement id (if any). E.g: <p>db.InsertIntoSelect<Contact>(db.From<Person>().Select(x => new { x.Id, Surname == x.LastName }))</p>

View Source
Declaration
public static long InsertIntoSelect<T>(this IDbConnection dbConn, ISqlExpression query, Action<IDbCommand> commandFilter)
Returns

System.Int64

Parameters
TypeName
IDbConnectiondbConn
ServiceStack.OrmLite.ISqlExpressionquery
Action<IDbCommand>commandFilter
Type Parameters
  • T

InsertAll<T>(IDbConnection, IEnumerable<T>)

Insert a collection of POCOs in a transaction. E.g: <p>db.InsertAll(new[] { new Person { Id = 9, FirstName = "Biggie", LastName = "Smalls", Age = 24 } })</p>

View Source
Declaration
public static void InsertAll<T>(this IDbConnection dbConn, IEnumerable<T> objs)
Parameters
TypeName
IDbConnectiondbConn
IEnumerable<<T>>objs
Type Parameters
  • T

InsertAll<T>(IDbConnection, IEnumerable<T>, Action<IDbCommand>)

Insert a collection of POCOs in a transaction and modify populated IDbCommand with a commandFilter. E.g: <p>db.InsertAll(new[] { new Person { Id = 9, FirstName = "Biggie", LastName = "Smalls", Age = 24 } },</p> <p> dbCmd => applyFilter(dbCmd))</p>

View Source
Declaration
public static void InsertAll<T>(this IDbConnection dbConn, IEnumerable<T> objs, Action<IDbCommand> commandFilter)
Parameters
TypeName
IDbConnectiondbConn
IEnumerable<<T>>objs
Action<IDbCommand>commandFilter
Type Parameters
  • T

Insert<T>(IDbConnection, T[])

Insert 1 or more POCOs in a transaction. E.g: <p>db.Insert(new Person { Id = 1, FirstName = "Tupac", LastName = "Shakur", Age = 25 },</p> <p> new Person { Id = 2, FirstName = "Biggie", LastName = "Smalls", Age = 24 })</p>

View Source
Declaration
public static void Insert<T>(this IDbConnection dbConn, params T[] objs)
Parameters
TypeName
IDbConnectiondbConn
<T>[]objs
Type Parameters
  • T

Insert<T>(IDbConnection, Action<IDbCommand>, T[])

Insert 1 or more POCOs in a transaction and modify populated IDbCommand with a commandFilter. E.g: <p>db.Insert(dbCmd => applyFilter(dbCmd),</p> <p> new Person { Id = 1, FirstName = "Tupac", LastName = "Shakur", Age = 25 },</p> <p> new Person { Id = 2, FirstName = "Biggie", LastName = "Smalls", Age = 24 })</p>

View Source
Declaration
public static void Insert<T>(this IDbConnection dbConn, Action<IDbCommand> commandFilter, params T[] objs)
Parameters
TypeName
IDbConnectiondbConn
Action<IDbCommand>commandFilter
<T>[]objs
Type Parameters
  • T

Update<T>(IDbConnection, T, Action<IDbCommand>)

Updates 1 POCO. All fields are updated except for the PrimaryKey which is used as the identity selector. E.g: <p>db.Update(new Person { Id = 1, FirstName = "Jimi", LastName = "Hendrix", Age = 27 })</p>

View Source
Declaration
public static int Update<T>(this IDbConnection dbConn, T obj, Action<IDbCommand> commandFilter = null)
Returns

System.Int32

Parameters
TypeName
IDbConnectiondbConn
<T>obj
Action<IDbCommand>commandFilter
Type Parameters
  • T

Update<T>(IDbConnection, Dictionary<String, Object>, Action<IDbCommand>)

Updates 1 POCO. All fields are updated except for the PrimaryKey which is used as the identity selector. E.g: <p>db.Update(new Dictionary<string,object> { ["Id"] = 1, ["FirstName"] = "Jimi" })</p>

View Source
Declaration
public static int Update<T>(this IDbConnection dbConn, Dictionary<string, object> obj, Action<IDbCommand> commandFilter = null)
Returns

System.Int32

Parameters
TypeName
IDbConnectiondbConn
Dictionary<System.String,System.Object>obj
Action<IDbCommand>commandFilter
Type Parameters
  • T

Update<T>(IDbConnection, T[])

Updates 1 or more POCOs in a transaction. E.g: <p>db.Update(new Person { Id = 1, FirstName = "Tupac", LastName = "Shakur", Age = 25 },</p> <p>new Person { Id = 2, FirstName = "Biggie", LastName = "Smalls", Age = 24 })</p>

View Source
Declaration
public static int Update<T>(this IDbConnection dbConn, params T[] objs)
Returns

System.Int32

Parameters
TypeName
IDbConnectiondbConn
<T>[]objs
Type Parameters
  • T

Update<T>(IDbConnection, Action<IDbCommand>, T[])

View Source
Declaration
public static int Update<T>(this IDbConnection dbConn, Action<IDbCommand> commandFilter, params T[] objs)
Returns

System.Int32

Parameters
TypeName
IDbConnectiondbConn
Action<IDbCommand>commandFilter
<T>[]objs
Type Parameters
  • T

UpdateAll<T>(IDbConnection, IEnumerable<T>, Action<IDbCommand>)

Updates 1 or more POCOs in a transaction. E.g: <p>db.UpdateAll(new[] { new Person { Id = 1, FirstName = "Jimi", LastName = "Hendrix", Age = 27 } })</p>

View Source
Declaration
public static int UpdateAll<T>(this IDbConnection dbConn, IEnumerable<T> objs, Action<IDbCommand> commandFilter = null)
Returns

System.Int32

Parameters
TypeName
IDbConnectiondbConn
IEnumerable<<T>>objs
Action<IDbCommand>commandFilter
Type Parameters
  • T

Delete<T>(IDbConnection, Object, Action<IDbCommand>)

Delete rows using an anonymous type filter. E.g: <p>db.Delete<Person>(new { FirstName = "Jimi", Age = 27 })</p>

View Source
Declaration
public static int Delete<T>(this IDbConnection dbConn, object anonFilter, Action<IDbCommand> commandFilter = null)
Returns

System.Int32: number of rows deleted

Parameters
TypeName
IDbConnectiondbConn
System.ObjectanonFilter
Action<IDbCommand>commandFilter
Type Parameters
  • T

Delete<T>(IDbConnection, Dictionary<String, Object>)

Delete rows using an Object Dictionary filters. E.g: <p>db.Delete<Person>(new Dictionary<string,object> { ["FirstName"] = "Jimi", ["Age"] = 27 })</p>

View Source
Declaration
public static int Delete<T>(this IDbConnection dbConn, Dictionary<string, object> filters)
Returns

System.Int32: number of rows deleted

Parameters
TypeName
IDbConnectiondbConn
Dictionary<System.String,System.Object>filters
Type Parameters
  • T

Delete<T>(IDbConnection, T, Action<IDbCommand>)

Delete 1 row using all fields in the filter. E.g: <p>db.Delete(new Person { Id = 1, FirstName = "Jimi", LastName = "Hendrix", Age = 27 })</p>

View Source
Declaration
public static int Delete<T>(this IDbConnection dbConn, T allFieldsFilter, Action<IDbCommand> commandFilter = null)
Returns

System.Int32: number of rows deleted

Parameters
TypeName
IDbConnectiondbConn
<T>allFieldsFilter
Action<IDbCommand>commandFilter
Type Parameters
  • T

Delete<T>(IDbConnection, T[])

Delete 1 or more rows in a transaction using all fields in the filter. E.g: <p>db.Delete(new Person { Id = 1, FirstName = "Jimi", LastName = "Hendrix", Age = 27 })</p>

View Source
Declaration
public static int Delete<T>(this IDbConnection dbConn, params T[] allFieldsFilters)
Returns

System.Int32

Parameters
TypeName
IDbConnectiondbConn
<T>[]allFieldsFilters
Type Parameters
  • T

DeleteNonDefaults<T>(IDbConnection, T)

Delete 1 or more rows using only field with non-default values in the filter. E.g: <p>db.DeleteNonDefaults(new Person { FirstName = "Jimi", Age = 27 })</p>

View Source
Declaration
public static int DeleteNonDefaults<T>(this IDbConnection dbConn, T nonDefaultsFilter)
Returns

System.Int32: number of rows deleted

Parameters
TypeName
IDbConnectiondbConn
<T>nonDefaultsFilter
Type Parameters
  • T

DeleteNonDefaults<T>(IDbConnection, T[])

Delete 1 or more rows in a transaction using only field with non-default values in the filter. E.g: <p>db.DeleteNonDefaults(new Person { FirstName = "Jimi", Age = 27 }, new Person { FirstName = "Janis", Age = 27 })</p>

View Source
Declaration
public static int DeleteNonDefaults<T>(this IDbConnection dbConn, params T[] nonDefaultsFilters)
Returns

System.Int32: number of rows deleted

Parameters
TypeName
IDbConnectiondbConn
<T>[]nonDefaultsFilters
Type Parameters
  • T

DeleteById<T>(IDbConnection, Object, Action<IDbCommand>)

Delete 1 row by the PrimaryKey. E.g: <p>db.DeleteById<Person>(1)</p>

View Source
Declaration
public static int DeleteById<T>(this IDbConnection dbConn, object id, Action<IDbCommand> commandFilter = null)
Returns

System.Int32: number of rows deleted

Parameters
TypeName
IDbConnectiondbConn
System.Objectid
Action<IDbCommand>commandFilter
Type Parameters
  • T

DeleteById<T>(IDbConnection, Object, UInt64, Action<IDbCommand>)

Delete 1 row by the PrimaryKey where the rowVersion matches the optimistic concurrency field. Will throw ServiceStack.Data.OptimisticConcurrencyException?text=RowModifiedException if the row does not exist or has a different row version. E.g: <p>db.DeleteById<Person>(1)</p>

View Source
Declaration
public static void DeleteById<T>(this IDbConnection dbConn, object id, ulong rowVersion, Action<IDbCommand> commandFilter = null)
Parameters
TypeName
IDbConnectiondbConn
System.Objectid
System.UInt64rowVersion
Action<IDbCommand>commandFilter
Type Parameters
  • T

DeleteByIds<T>(IDbConnection, IEnumerable)

Delete all rows identified by the PrimaryKeys. E.g: <p>db.DeleteById<Person>(new[] { 1, 2, 3 })</p>

View Source
Declaration
public static int DeleteByIds<T>(this IDbConnection dbConn, IEnumerable idValues)
Returns

System.Int32: number of rows deleted

Parameters
TypeName
IDbConnectiondbConn
IEnumerableidValues
Type Parameters
  • T

DeleteAll<T>(IDbConnection)

Delete all rows in the generic table type. E.g: <p>db.DeleteAll<Person>()</p>

View Source
Declaration
public static int DeleteAll<T>(this IDbConnection dbConn)
Returns

System.Int32: number of rows deleted

Parameters
TypeName
IDbConnectiondbConn
Type Parameters
  • T

DeleteAll<T>(IDbConnection, IEnumerable<T>)

Delete all rows provided. E.g: <p>db.DeleteAll<Person>(people)</p>

View Source
Declaration
public static int DeleteAll<T>(this IDbConnection dbConn, IEnumerable<T> rows)
Returns

System.Int32: number of rows deleted

Parameters
TypeName
IDbConnectiondbConn
IEnumerable<<T>>rows
Type Parameters
  • T

DeleteAll(IDbConnection, Type)

Delete all rows in the runtime table type. E.g: <p>db.DeleteAll(typeof(Person))</p>

View Source
Declaration
public static int DeleteAll(this IDbConnection dbConn, Type tableType)
Returns

System.Int32: number of rows deleted

Parameters
TypeName
IDbConnectiondbConn
TypetableType

Delete<T>(IDbConnection, String, Object)

Delete rows using a SqlFormat filter. E.g: <p>db.Delete<Person>("Age > @age", new { age = 42 })</p>

View Source
Declaration
public static int Delete<T>(this IDbConnection dbConn, string sqlFilter, object anonType)
Returns

System.Int32: number of rows deleted

Parameters
TypeName
IDbConnectiondbConn
System.StringsqlFilter
System.ObjectanonType
Type Parameters
  • T

Delete(IDbConnection, Type, String, Object)

Delete rows using a SqlFormat filter. E.g: <p>db.Delete(typeof(Person), "Age > @age", new { age = 42 })</p>

View Source
Declaration
public static int Delete(this IDbConnection dbConn, Type tableType, string sqlFilter, object anonType)
Returns

System.Int32: number of rows deleted

Parameters
TypeName
IDbConnectiondbConn
TypetableType
System.StringsqlFilter
System.ObjectanonType

Save<T>(IDbConnection, T, Boolean)

Insert a new row or update existing row. Returns true if a new row was inserted. Optional references param decides whether to save all related references as well. E.g: <p>db.Save(customer, references:true)</p>

View Source
Declaration
public static bool Save<T>(this IDbConnection dbConn, T obj, bool references = false)
Returns

System.Boolean: true if a row was inserted; false if it was updated

Parameters
TypeName
IDbConnectiondbConn
<T>obj
System.Booleanreferences
Type Parameters
  • T

Save<T>(IDbConnection, T[])

Insert new rows or update existing rows. Return number of rows added E.g: <p>db.Save(new Person { Id = 10, FirstName = "Amy", LastName = "Winehouse", Age = 27 })</p>

View Source
Declaration
public static int Save<T>(this IDbConnection dbConn, params T[] objs)
Returns

System.Int32: number of rows added

Parameters
TypeName
IDbConnectiondbConn
<T>[]objs
Type Parameters
  • T

SaveAll<T>(IDbConnection, IEnumerable<T>)

Insert new rows or update existing rows. Return number of rows added E.g: <p>db.SaveAll(new [] { new Person { Id = 10, FirstName = "Amy", LastName = "Winehouse", Age = 27 } })</p>

View Source
Declaration
public static int SaveAll<T>(this IDbConnection dbConn, IEnumerable<T> objs)
Returns

System.Int32: number of rows added

Parameters
TypeName
IDbConnectiondbConn
IEnumerable<<T>>objs
Type Parameters
  • T

SaveAllReferences<T>(IDbConnection, T)

Populates all related references on the instance with its primary key and saves them. Uses '(T)Id' naming convention. E.g: <p>db.SaveAllReferences(customer)</p>

View Source
Declaration
public static void SaveAllReferences<T>(this IDbConnection dbConn, T instance)
Parameters
TypeName
IDbConnectiondbConn
<T>instance
Type Parameters
  • T

SaveReferences<T, TRef>(IDbConnection, T, TRef[])

Populates the related references with the instance primary key and saves them. Uses '(T)Id' naming convention. E.g: <p>db.SaveReference(customer, customer.Orders)</p>

View Source
Declaration
public static void SaveReferences<T, TRef>(this IDbConnection dbConn, T instance, params TRef[] refs)
Parameters
TypeName
IDbConnectiondbConn
<T>instance
<TRef>[]refs
Type Parameters
  • T
  • TRef

SaveReferences<T, TRef>(IDbConnection, T, List<TRef>)

Populates the related references with the instance primary key and saves them. Uses '(T)Id' naming convention. E.g: <p>db.SaveReference(customer, customer.Orders)</p>

View Source
Declaration
public static void SaveReferences<T, TRef>(this IDbConnection dbConn, T instance, List<TRef> refs)
Parameters
TypeName
IDbConnectiondbConn
<T>instance
List<<TRef>>refs
Type Parameters
  • T
  • TRef

SaveReferences<T, TRef>(IDbConnection, T, IEnumerable<TRef>)

Populates the related references with the instance primary key and saves them. Uses '(T)Id' naming convention. E.g: <p>db.SaveReferences(customer, customer.Orders)</p>

View Source
Declaration
public static void SaveReferences<T, TRef>(this IDbConnection dbConn, T instance, IEnumerable<TRef> refs)
Parameters
TypeName
IDbConnectiondbConn
<T>instance
IEnumerable<<TRef>>refs
Type Parameters
  • T
  • TRef

GetRowVersion<T>(IDbConnection, Object)

View Source
Declaration
public static object GetRowVersion<T>(this IDbConnection dbConn, object id)
Returns

System.Object

Parameters
TypeName
IDbConnectiondbConn
System.Objectid
Type Parameters
  • T

GetRowVersion(IDbConnection, Type, Object)

View Source
Declaration
public static object GetRowVersion(this IDbConnection dbConn, Type modelType, object id)
Returns

System.Object

Parameters
TypeName
IDbConnectiondbConn
TypemodelType
System.Objectid

ExecuteProcedure<T>(IDbConnection, T)

View Source
Declaration
public static void ExecuteProcedure<T>(this IDbConnection dbConn, T obj)
Parameters
TypeName
IDbConnectiondbConn
<T>obj
Type Parameters
  • T

ToUpdateStatement<T>(IDbConnection, T, ICollection<String>)

Generates inline UPDATE SQL Statement

View Source
Declaration
public static string ToUpdateStatement<T>(this IDbConnection dbConn, T item, ICollection<string> updateFields = null)
Returns

System.String

Parameters
TypeName
IDbConnectiondbConn
<T>item
ICollection<System.String>updateFields
Type Parameters
  • T

ToInsertStatement<T>(IDbConnection, T, ICollection<String>)

Generates inline INSERT SQL Statement

View Source
Declaration
public static string ToInsertStatement<T>(this IDbConnection dbConn, T item, ICollection<string> insertFields = null)
Returns

System.String

Parameters
TypeName
IDbConnectiondbConn
<T>item
ICollection<System.String>insertFields
Type Parameters
  • T