-
Notifications
You must be signed in to change notification settings - Fork 128
Home
using Dapper.FastCrud
OrmConfiguration.DefaultDialect = SqlDialect.MsSql|MySql|SqLite|PostgreSql
dbConnection.Insert(newEntity);
newEntity
will have its properties partially updated with the db generated values on return
dbConnection.Get(new Asset {Id = 10});
dbConnection.Find<Entity>();
var selectParams = new {
FirstName = "John"
}
dbConnection.Find<Entity>(statement => statement
.Where($"{nameof(Entity.FirstName):C} = {nameof(selectParams.FirstName):P}")
.OrderBy($"{nameof(Entity.LastName):C} DESC")
.Skip(10)
.Top(20)
.WithParameters(selectparams);
:C and :P used here are string formatter specifiers. Please refer to SQL statements and clauses for all the available formatter specifiers.
dbConnection.Update(updatedEntity);
updatedEntity
will have its properties partially updated with the db generated values on return
dbConnection.BulkUpdate(new Asset {Name = "Unknown"});
- Option 1: Create a new temporary mapping for partial updating the entities.
var bulkUpdateParams = new {
AssetName = "workstation"
};
var partialUpdateMapping = OrmConfiguration.GetDefaultEntityMapping<EmployeeDbEntity>
.UpdatePropertiesExcluding(prop => prop.IncludeInUpdates(false),
nameof(Asset.IsLost));
dbConnection.BulkUpdate(new Asset {IsLost = true}, statement => statement
.Where($"{nameof(Asset.Name):C} = {nameof(bulkUpdateParams.AssetName):P}"))
.WithEntityMappingOverride(partialUpdateMapping);
-
Option 2: Create a new entity pointing to the same table, having just the primary keys and the properties you require for the update, that can be used strictly for bulk updates.
-
Option 3: Create your own statement using FastCrud's built-in SQL formatter and run it straight through Dapper. That way you benefit from all the mappings done through FastCrud and the FastCrud's formatter, while still having the flexibility of creating your own SQL statements.
string statement = Sql.Format<Asset>($@"
UPDATE {nameof(Asset):T}
SET {nameof(Asset.IsLost):C} = 1
WHERE {nameof(Asset.Name):C} = {nameof(bulkUpdateParams.AssetName):P}
");
dbConnection.Delete(new Asset {Id = 10});
dbConnection.BulkDelete<Entity>();
dbConnection.BulkDelete<Asset>(statement => statement
.Where($"{nameof(Asset.IsLost):C}=1"));
dbConnection.Count<Entity>();
var countParams = new {
AssetName = "workstation";
};
dbConnection.Count<Asset>(statement => statement
.Where($"{nameof(Asset.Name):C} = {nameof(countParams.AssetName:P}")
.WithParameters(countParams));
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)));