Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Dapper be used to update and insert models?

Tags:

c#

.net

sql

dapper

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!

like image 563
Casey Crookston Avatar asked Mar 24 '16 13:03

Casey Crookston


People also ask

Does Dapper support bulk insert?

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.

Why we should use Dapper?

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.

Why Dapper is micro ORM?

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.

Is Dapper A ORM?

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.


2 Answers

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; }
} 
like image 97
Robot Mess Avatar answered Oct 22 '22 18:10

Robot Mess


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);
like image 39
Felice Pollano Avatar answered Oct 22 '22 19:10

Felice Pollano