How to map the return of a Value Object with Dapper?

Asked

Viewed 909 times

0

Hello!

I have a question related to the return of an object with an already filled 'Value Object', 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; }
}

Class Email.Cs

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

In any search I do with Dapper the return of Email is always null. There is some way to map the return of Dapper?

Note. The data is persisted in a single table, there is no relationship.

1 answer

1


It’s simple

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

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();
}

inserir a descrição da imagem aqui

Check the query if it meets the columns of your DB, in my article explains in detail!

On my Github there’s more about: https://github.com/thiagoloureiro/Dapper_ComplexObjects

I also have an article on: https://medium.com/@thiagoloureiro/Dapper-objects-complexes-5e5fae83caa7

  • Hello Thiago, thank you for answering. In my case there is no such relationship for Join, the table would be with the following columns: 'Userid, Name, Password and Email' How can I map to the Email column to be populated in the Email Property.Address?

  • I tried to use Dommel + Fluentmap but I was also unsuccessful. Any ideas? Thanks

  • Leandro, why don’t you just put the email column as string ? In Model I say.. public string Email { get; set; } E ready.. will solve your problem.. this Email class makes no sense.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.