'is it possible to run a raw sql using ef core 5 with inner join and materialize the data to a class?

public class UserDto
{
    public int Id {get; set;}
    public string Name {get; set;}
    public string Email {get; set;}
    public string Username {get; set;}
}

// code first entity class

public class User
{
    public int Id {get; set;}
    public string Username {get; set;}
}

// code first entity class

public class Profile
{
    public string Name {get; set;}
    public string Email {get; set;}
}

// i want to do something like this

List<UserDto> userDto = context.Database
.FromSqlRaw<List<UserDto>>("SELECT u.Id, u.Username, p.Name, p.Email FROM dbo.User u
    INNER JOIN dbo.Profile p on p.UserId = u.id 
    ").ToList();


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source