efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Unable to use FromSqlRaw

Open medhatelmasry opened this issue 6 years ago • 1 comments

I am using .NET Core 3.0.100. When I execute this C# statement

        var data = db.Products.FromSqlRaw("[dbo].[Ten Most Expensive Products]")
            .ToList();

I get the following error:

        The required column 'ProductID' was not present in the results of a 'FromSql' operation.

I am using the sample Northwind database and the Stored Procedure looks like this:

 create procedure [dbo].[Ten Most Expensive Products] AS
 SET ROWCOUNT 10
 SELECT Products.ProductName AS TenMostExpensiveProducts, Products.UnitPrice
 FROM Products
 ORDER BY Products.UnitPrice DESC

Why is it necessary for the results to have a ProductID??

Steps to reproduce

Further technical details

EF Core version: 3.1.0 Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: 3.0.100 Operating system: Mac IDE: VS Code

medhatelmasry avatar Jan 10 '20 23:01 medhatelmasry

@medhatelmasry Because Products.FromSqlRaw is going to create full Product entity instances and hence needs get values for all mapped properties. See https://docs.microsoft.com/en-us/ef/core/querying/raw-sql and https://docs.microsoft.com/en-us/ef/core/modeling/keyless-entity-types

ajcvickers avatar Jan 13 '20 17:01 ajcvickers