-
Notifications
You must be signed in to change notification settings - Fork 1
Using Dapper.CX Extension Methods
Adam O'Neil edited this page Sep 22, 2020
·
3 revisions
After installing NuGet package Dapper.CX.SqlServer, add either the Dapper.CX.SqlServer.Extensions.Int or Dapper.CX.SqlServer.Extensions.Long namespace in your classes. One way to do that is to start typing a Dapper.CX method name, and pick the desired namespace from the Ctrl+dot popup.

Then you can start using all the Dapper.CX methods.
Here's a simple example using GetAsync and SaveAsync methods assuming a fictional Appointment model class and fictional GetConnection method:
using (var cn = GetConnection())
{
var appt = await cn.GetAsync<Appointment>(id);
// make your changes
await cn.SaveAsync(appt);
}The SaveAsync method performs an insert or update depending on whether the model object IsNew or not. Here's a more sophisticated example showing the GetWhereAsync method and ChangeTracker object. Learn more about tracking changes.
using (var cn = GetConnection())
{
var appt = await cn.GetWhereAsync<Appointment>(new { clientId = 4343, date = new DateTime(2020, 3, 1) });
var ct = new ChangeTracker<Appointment>(appt);
// make your changes
// with a change tracker object, only modified properties are included in update statement
await cn.SaveAsync(appt, ct);
}