Home

Stefan Kip edited this page Sep 26, 2017 · 44 revisions

Table of Contents

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

Select by primary key(s)
dbConnection.Get(new Asset {Id = 10});
Select All
dbConnection.Find<Entity>();
Select record set
    dbConnection.Find<Entity>(statement => statement  
                .Where($"{nameof(Entity.FirstName):C}=@FirstNameParam")  
                .OrderBy($"{nameof(Entity.LastName):C} DESC")  
                .Skip(10)  
                .Top(20)  
                .WithParameters(new {FirstNameParam = "John"});

:C is a string formatter, refer to SQL statements and clauses for available formatters.

Update

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):C}='Unknown'"));

Delete

Delete by primary key(s)
dbConnection.Delete(new Asset {Id = 10});
Delete all
dbConnection.BulkDelete<Entity>();
Delete record set
    dbConnection.BulkDelete<Asset>(statement => statement  
      .Where($"{nameof(Asset.IsLost):C}=1"));

Count

Count all
    dbConnection.Count<Entity>();
Count record set
    dbConnection.Count<Asset>(statement => statement  
                .Where($"{nameof(Asset.Name):C}=@AssetNameParam")  
                .WithParameters(new {AssetNameParam=assetName}));

Async

Async methods have an identical usage


Options

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: - Installation - Entity registration - Multiple mappings - SQL statements and clauses - Default library conventions - JOINs