How to map return of a Value Object with Dapper?

0

Hello!

I have a question regarding the return of an object with an already-filled 'Value Object', for example:

I have the User.cs class

public class User
{
    public Guid UserId { get; set; }
    public string Name { get; set; }
    public string Password { get; set; }
    public Email Email { get; set; }
}

Email.cs class

public class Email
{
    public string Address { get; set; }
}

In any search I do with Dapper the return of Email always comes null. Is there any way to map the Dapper return?

Obs. The data is persisted into a single table, there is no relationship.

    
asked by anonymous 11.04.2018 / 18:00

1 answer

1

It's simple

Edit , I did without the relationship the way you said it:

List<User> ret;
using (var db = new SqlConnection(connstring))
{
    var sql =
        "select UserId, Name, Password, Email as Address from [User]";

    ret = db.Query<User, Email, User>(sql, (user, email) =>
    {
        user.Email = email;
        return user;
    }, splitOn: "Address").ToList();
}

CheckthequeryifitmeetsthecolumnsofyourDB,inmyarticleitexplainsindetail!

InmyGithubyouhavemoreabout: link

I also have an article about: link

    
11.04.2018 / 20:02