简体   繁体   English

在Dapper中映射实体

[英]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: 我刚刚开始使用Dapper,我似乎找不到像将数据映射到数据库中的表那样简单的事情:

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? 我的问题是:如何告诉我的实体用户我的数据库中的Id是Userid?

In EF I would do something like this: 在EF我会做这样的事情:

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? 如何在短小精悍的情况下实现上述目标?

Dapper deliberately doesn't have a mapping layer; Dapper故意没有映射层; 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: 但是,如果我正确理解您不想在TSQL中使用别名,并且不需要任何pass-thru属性 - 那么请使用非泛型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. 这里的技巧是非通用的Query(...) API使用dynamic ,每列名称提供成员。

It can't, your user class must be defined to match the result coming back from the query. 它不能,必须定义您的用户类以匹配从查询返回的结果。

Once you've got the result back you must map it manually to another class (or use AutoMapper) 一旦得到结果,你必须手动将它映射到另一个类(或使用AutoMapper)

You could try something like this: 你可以尝试这样的事情:

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

    #region Remappings

    public int UserId
    {
        get { return Id; }
        set { Id = value; }
    }

    #endregion
}

It might be overkill for your example, but I've found it useful in some situations to avoid cluttering every Query<> call with the remapping code. 对于您的示例可能有点过分,但我发现在某些情况下它有用,可以避免使用重新映射代码混乱每个Query <>调用。

I would recommend NReco to you. 我会向你推荐NReco。 It is performant like dapper but with easy mapping with attributes. 它像dapper一样高性能,但易于使用属性进行映射。 nreco nreco

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM