Pomelo.EntityFrameworkCore.MySql icon indicating copy to clipboard operation
Pomelo.EntityFrameworkCore.MySql copied to clipboard

FromSql to FromSqlRaw - migrate 2.2.6 to 3.1.1 - netcore-3.1

Open virtualdreams opened this issue 5 years ago • 5 comments

Steps to reproduce

Upgrade package from 2.2.6 to 3.1.1 results in an error.

The issue

Before i change from FromSql to FromSqlRaw:

'RelationalQueryableExtensions.FromSql<TEntity>(IQueryable<TEntity>, FormattableString)' is obsolete: 'For returning objects from SQL queries using plain strings, use FromSqlRaw instead. For returning objects from SQL queries using interpolated string syntax to create parameters, use FromSqlInterpolated instead. Call either new method directly on the DbSet at the root of the query.' 

After i change from FromSql to FromSqlRaw:

'IQueryable<Note>' does not contain a definition for 'FromSqlRaw' and no accessible extension method 'FromSqlRaw' accepting a first argument of type 'IQueryable<Note>' could be found (are you missing a using directive or an assembly reference?)

Source sample:

var _query = Context.Note.AsQueryable();
_query = _query.FromSqlRaw($@"
select * from anywhere
");

Question

How to fix this issue and where is migration path or documentation about?

Further technical details

Operating system: Arch Linux Pomelo.EntityFrameworkCore.MySql version: 3.1.1 Microsoft.AspNetCore.App version: 3.1.2 (official from MS)

virtualdreams avatar Feb 22 '20 17:02 virtualdreams

[...] where is migration path or documentation about?

You can find everything about Raw SQL Queries in the EF Core docs.

How to fix this issue [...]

As the warning says, you need to

Call either new method directly on the DbSet at the root of the query.`

So for your example, the following should work:

var _query = Context.Note.FromSqlRaw($@"select * from anywhere");

The docs about the FromSqlRaw method state correctly, that the first parameter is of type DbSet<TEntity>:

this DbSet<TEntity> source

Therefore, you need to call this extension method on a DbSet<TEntity> object. It seems though, that the description of the parameter is inaccurate, because it says:

An IQueryable<T> to use as the base of the raw SQL query (typically a DbSet<TEntity>).

The same is stated for the FromSqlInterpolated method. This seems to be a copy & paste error in the docs, because the obsolete FromSql method was an extension method for IQueryable<TEntity> instead for DbSet<TEntity> and the description was probably just reused (/cc @ajcvickers).


Concrete upgrade documentation for FromSql can be found in the Raw SQL Queries article at the end.

General new and breaking changes can be found in the following (long) articles of the EF Core docs:

lauxjpn avatar Feb 22 '20 17:02 lauxjpn

Thanks. AsQueryable() was the problem here. Removing this causes some minor problems for me, but it works now with:

IQueryable<Note> _query = null;
// some other code
_query = Context.Note.FromSqlInterpolated($@"select * from anywhere where x = {value}");
// some other code
_query.ToListAsync();

virtualdreams avatar Feb 22 '20 18:02 virtualdreams

Let's keep this issue open until the docs for EF Core have been fixed.

lauxjpn avatar Feb 24 '20 11:02 lauxjpn

@ajcvickers Just a quick reminder to fix the EF Core docs.

lauxjpn avatar Mar 09 '20 19:03 lauxjpn

@ajcvickers I just wanted to correct this in the api reference docs myself, but I couldn't find them neither on the https://github.com/dotnet/dotnet-api-docs nor on the https://github.com/dotnet/EntityFramework.Docs repo.

Are the EF Core api reference docs hosted somewhere else?

lauxjpn avatar Apr 10 '20 21:04 lauxjpn