Skip to content
MoonStorm edited this page Feb 2, 2016 · 64 revisions

Usage

  • using Dapper.FastCrud
  • OrmConfiguration.DefaultDialect = SqlDialect.MsSql|MySql|SqLite|PostgreSql
  • Insert:
    dbConnection.Insert(newEntity);
    newEntity will have its properties partially updated with the db generated values on return
  • Select by primary key(s):
    dbConnection.Get(new Asset {Id = 10});
  • Select all:
    dbConnection.Find();
  • Select record set: dbConnection.Find(statement => statement
    .Where($"{nameof(Entity.FirstName):C}=@FirstNameParam")
    .OrderBy($"{nameof(Entity.LastName):C} DESC")
    .Skip(10)
    .Top(20)
    .WithParameters(new {FirstNameParam: "John"});
  • Update by primary key(s):
    dbConnection.Update(updatedEntity);
    updatedEntity will have its properties partially updated with the db generated values on return
  • Update all:
    dbConnection.BulkUpdate(new Asset {Name = "Unknown"});
  • Update record set:
    dbConnection.BulkUpdate(new Asset {IsLost = true}, statement => statement
    .Where(@"{nameof(Asset.Name)='Unknown'}"));
  • Delete by primary key(s):
    dbConnection.Delete(new Asset {Id = 10});
  • Delete all:
    dbConnection.BulkDelete();
  • Delete record set:
    dbConnection.BulkDelete(statement => statement
    .Where($"{nameof(Asset.IsLost)=1}"));
  • Count all:
    dbConnection.Count();
  • Count record set: dbConnection.Count(statement => statement
    .Where($"{nameof(Asset.Name):C}=@AssetNameParam")
    .WithParameters(new {AssetNameParam=assetName}));

async methods have an identical usage


As you've noticed in the previous examples, a number of options are immediately available for tweaking the statement. Their availability varies by type of operation.

  • WithTimeout() - Note: the default timeout can be set instead via OrmConfiguration.DefaultSqlStatementOptions
  • AttachToTransaction()
  • WithEntityMappingOverride()
  • Top()
  • OrderBy()
  • Skip()
  • StreamResults()
  • Where()
  • WithParameters()

e.g.:

dbConnection.Insert(entityToInsert, statement =>statement    
  .AttachToTransaction(dbTransaction)
  .WithTimeout(TimeSpan.FromSeconds(10)));

For more information, please check the following sections: