efcore
efcore copied to clipboard
Unable to use FromSqlRaw
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 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