2

I'm refactoring a code and I have this question.

public class MovimentoManualListRepository : RepositoryBase<MovimentoManualList>, IMovimentoManualListRepository
{
    ConsultarContext _context;
    public MovimentoManualListRepository(ConsultarContext context) : base(context)
    {
        _context = context;
    }

    public List<MovimentoManualList> Listar()
    {
        return _context.Database
            .SqlQuery<MovimentoManualList>("ListarMovimentacao")
            .ToList();
    }
}

I get an error:

Severity Code Description Project File Line Suppression State Error CS1061 'DatabaseFacade' does not contain a definition for 'SqlQuery' and no accessible extension method 'SqlQuery' accepting a first argument of type 'DatabaseFacade' could be found (are you missing a using directive or an assembly reference?)

marc_s
  • 704,970
  • 168
  • 1,303
  • 1,425
Thiago Cruz
  • 55
  • 1
  • 9

1 Answers1

1

Do you mean FromSqlRaw.?

Basic raw SQL queries

You can 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<>.

var blogs = context.Blogs
   .FromSqlRaw("SELECT * FROM dbo.Blogs")
   .ToList();

The following executes a stored procedure

var blogs = context.Blogs
    .FromSqlRaw("EXECUTE dbo.GetMostPopularBlogs")
    .ToList();

Please note that:

  • it can be mixed and matched it with Linq queries
  • there are limitations

Please see Raw SQL Queries.

marc_s
  • 704,970
  • 168
  • 1,303
  • 1,425
tymtam
  • 24,269
  • 5
  • 71
  • 101