Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping entity in Dapper

I've just started working with Dapper and I don't seem to find something very simple like mapping an entity to a table in my database:

I have a stored procedure:

CREATE PROCEDURE [dbo].GetUserById (@UserId int)
AS  
begin               
        SELECT UserId,LastName,FirstName,EmailAddress
        FROM users
        WHERE UserID = @UserId

end
go

Then an entity:

public class User
{
    public int Id { get; set; }
    public string LastName { get; set; }
    public string FirstName { get; set; }
    public string Email { get; set; }
}

And a dapper query in my code:

int userid=1;
    User User = connection.Query<User>("#GetUserById", new {userid=userid}, commandType: CommandType.StoredProcedure).FirstOrDefault();

My question is: How can I tell my entity User that Id is Userid on my database?

In EF I would do something like this:

MapSingleType(c => new
            {
                UserId = c.Id,
                Firstname = c.Firstname,
                Lastname = c.Lastname,
                EmailAddress = c.Email
            }).ToTable("users");

How can the above be achieved in dapper?

like image 379
David Aleu Avatar asked Mar 01 '12 14:03

David Aleu


People also ask

How does Dapper mapping work?

Dapper maps data to the first type in the same way as it does if only one generic parameter has been supplied to the QueryAsync<T> method. If is then told to map data to the Category type, and to assign the resulting object to the product's Category property.

What is splitOn in dapper?

splitOn: CustomerId will result in a null customer name. If you specify CustomerId,CustomerName as split points, dapper assumes you are trying to split up the result set into 3 objects. First starts at the beginning, second starts at CustomerId , third at CustomerName .

Is Dapper better than Entity Framework?

Dapper is literally much faster than Entity Framework Core considering the fact that there are no bells and whistles in Dapper. It is a straight forward Micro ORM that has minimal features as well. It is always up to the developer to choose between these 2 Awesome Data Access Technologies.

When using the multi mapping APIs ensure?

c# - When using the multi-mapping APIs ensure you set the splitOn param if you have keys other than Id", "splitOn - Stack Overflow. Stack Overflow for Teams – Start collaborating and sharing organizational knowledge.


1 Answers

Dapper deliberately doesn't have a mapping layer; it is the absolute minimum that can work, and frankly covers the majority of real scenarios in the process. However, if I understand correctly that you don't want to alias in the TSQL, and don't want any pass-thru properties - then use the non-generic Query API:

User user = connection.Query("...", ...).Select(obj => new User {
           Id = (int) obj.UserId,
           FirstName = (string) obj.FirstName,
           LastName = (string) obj.LastName,
           Email = (string) obj.EmailAddress
        }).FirstOrDefault();

or perhaps more simply in the case of a single record:

var obj = connection.Query("...", ...).FirstOrDefault();
User user = new User {
      Id = (int) obj.UserId,
      FirstName = (string) obj.FirstName,
      LastName = (string) obj.LastName,
      Email = (string) obj.EmailAddress
};

The trick here is that the non-generic Query(...) API uses dynamic, offering up members per column name.

like image 140
Marc Gravell Avatar answered Sep 17 '22 15:09

Marc Gravell