dotnet / dotnet/EntityFramework.Docs

FromSql method when used with stored procedure cannot be composed error

Open
#2,492 1 comment 0 reactions 0 assignees View on GitHub
area-query
Dominant language
Mermaid
Stars
1.7k
Forks
2k
Avg merge
7d 23h
Merged PRs (30d)
16

Description

It would be useful if this breaking changes document included how to handle the breaking change when amending EF code for stored procedures that return a single entity.

The suggested fix is to change
```
context.Products.FromSqlRaw("[dbo].[Ten Most Expensive Products]").FirstOrDefault();
```

To

```
context.Products.FromSqlRaw("[dbo].[Ten Most Expensive Products]").AsEnumerable().FirstOrDefault();
```

However whilst this works with tables it does not work for stored procedures where a single item is returned and no information on how to handle this is provided. After some trial and error I found this worked:
```
(await _context.Products.FromSqlInterpolated($"EXEC GetProducts").ToListAsync()).FirstOrDefault();
```

The issue does not apply for a list of items where this works:
```
await _context.Products.FromSqlInterpolated($"EXEC GetProducts").ToListAsync();
```

Could the docs be updated for next time I need to remind myself how I did this?

---
#### Document Details

⚠ *Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.*

* ID: e4d0e044-b0cb-21e2-19c1-2f137da671ac
* Version Independent ID: 1cbedd95-8640-1649-9967-d0ef71633534
* Content: [Breaking changes in EF Core 3.0 - EF Core](https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.0/breaking-changes#fromsqlsproc)
* Content Source: [entity-framework/core/what-is-new/ef-core-3.0/breaking-changes.md](https://github.com/dotnet/EntityFramework.Docs/blob/master/entity-framework/core/what-is-new/ef-core-3.0/breaking-changes.md)
* Product: **entity-framework**
* Technology: **entity-framework-core**
* GitHub Login: @ajcvickers
* Microsoft Alias: **avickers**

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.