I've been playing with Dapper, trying to see if it's a good light-weight alternative to Entity Framework. So far I've been impressed with it's ability to quickly pull an entity <model>
from a DB and get it into an IEnumerable of a model, or just create a single instance of a model. Very slick.
But what I'm not seeing is an easy way to update that model back to the db.
Example:
public class Dog
{
public Guid Id { get; set; }
public int? Age { get; set; }
public string Name { get; set; }
public float? Weight { get; set; }
}
We can easily create an IEnumerable of Dog as follows:
IEnumerable<Dog> dogs = connection.Query<Dog>("SELECT * FROM Dog")
Or, for a single instance:
Dog dog = connection.Query<Dog>("SELECT * FROM Dog WHERE DogId = @dogid")
That all works great. But now, if we make changes to "dog" (say, just change it's weight), is there a slick, fast, easy way to get that entity back into the database without having to do a manual UPDATE query, listing out each field?
Thanks!
Dapper provides the Execute method for inserting data. However, a database roundtrip is required for every data you insert. It's a perfect solution when you need to insert only 1 data but become very fast inefficient as soon as you insert multiple data.
Dapper as an ORM is arguably one of the most powerful tools for querying and executing databases. I use Dapper because it has no DB specific implementation detail and implementation works with MySQL, MongoDB, SQLite, SQL, and PostgreSQL. Dapper provides two methods of interacting with your database query and execute.
Dapper is an example of Micro ORM, in fact, it is called the King of Micro ORM because of its speed and ease of work. First, it creates an IDbConnection object and allows us to write queries to perform CRUD operations on the database.
Dapper is an object–relational mapping (ORM) product for the Microsoft . NET platform: it provides a framework for mapping an object-oriented domain model to a traditional relational database. Its purpose is to relieve the developer from a significant portion of relational data persistence-related programming tasks.
You can use the SqlMapperExtensions
class from Dapper.Contrib.Extensions
:
using Dapper;
using Dapper.Contrib.Extensions;
// don't forget the using since Update is an extension method
Dog entity; // you got it from somewhere
entity.Name = "fancy new dog name";
connection.Update(entity);
In order for this to work you need to add the [Key] annotation to your id. Should look something like this:
using System.ComponentModel.DataAnnotations;
public class Dog
{
[Key]
public long Id { get; set; }
public int? Age { get; set; }
public string Name { get; set; }
public float? Weight { get; set; }
}
Dapper is a Micro - Orm having as a characteristics being simple and fast. The behavior you describe is more related to fully fledged ORM, that imply having some sort off session concept, mapping, and query generation ( with hopefully drivers for different SQL flavours ). This is definitely not the spirit of Dapper, that anyway make the process of updating/inserting easyer that plain ado.net, accepting POCO object as parameters for your query, ie:
.Execute("update mydogs set age=@Age where id=@Id",dog);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With