How to execute sql and return to a `MODEL` in ef core?

mc 5,426 Reputation points
2021-07-14T02:49:27.477+00:00

I am using ef core 5.0 and want to execute a sql to return a list to a List<MyClass>

but the MyClass is not in the dbcontext's DbSet.

mysql is select newgroup,groupid from tmp

tmp is in DbSet,

public MyClass
{
 public int newgroup{get;set;}
 public int groupid{get;set;}
}
Developer technologies .NET Entity Framework Core
0 comments No comments
{count} votes

Accepted answer
  1. Daniel Zhang-MSFT 9,651 Reputation points
    2021-07-14T05:30:07.233+00:00

    Hi 6666666,
    You can try to use the FromSqlRaw extension method to begin a LINQ query based on a raw SQL query. FromSqlRaw can only be used on query roots, that is directly on the DbSet<>.
    More details please refer to this document.
    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.