String interpolation in raw SQL methods. How is this possible?

I just checked new futures in Entity Framework Core 2.0. It has a really nice feature called "String interpolation in raw SQL methods" which is described here .

It says that this code:

var city = "Redmond";

using (var context = CreateContext())
{
    context.Customers.FromSql($@"
        SELECT *
        FROM Customers
        WHERE City = {city}");
}

creates this request:

SELECT *
FROM Customers
WHERE City = @p0

This is really weird for me! How a method is FromSqlwritten as it is, and introduces a type string.

How does he understand that this is an interpolated string, and then create a parameter for it @p0? How can I write a method, for example FromSql, that knows how its string parameters are created?

+6
source share
1 answer

, woks FromSql( FormattableString.

$"...", FormatableString, { } , . { } placeholder @p0, , - new SqlParameter("@p0", formatableString.GetArgument(0))

+6

All Articles