question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support "Include" when using stored procedure in FromSql

See original GitHub issue

Suppose we have a DTO:

public class Person
{
    public string FirstName { get; set; }
    public int Id { get; set; }
    public string LastName { get; set; }
    public Pet Pet { get; set; }
    public int PetId { get; set; }
}

public class Pet
{
    public int Id { get; set; }
    public string Name { get; set; }
}

Currently running the code below throws “The Include operation is not supported when calling a stored procedure.”

db.Set<Person>().Include(p => p.Pet).FromSql("[dbo].[GetPersonsWithPets] @sortby = {0}", sortBy)

whereas if you use "SELECT Person.*, Pet.Name FROM Person INNER JOIN Pet on Person.PetId = Pet.Id", it works.

Can we get the same support for stored procedures? If they both return the same set of columns, it shouldn’t be an issue, should it?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
divegacommented, Oct 22, 2015

Assuming you have two stored procedures with the same filter criteria:

db.Set<Pets>().FromSql("[dbo].[GetPetsForPersons] @lastName = {0}", lastName).Load();
var persons = db.Set<Person>().FromSql("[dbo].[GetPersons] @lastName = {0}", lastName);

when iterating over persons you will see that the references on each person point to the right pets.

BTW, with SQL Server you should be able to write queries against Table-Valued Functions as well.

But fundamentally you cannot do this with a single query.

3reactions
maumarcommented, Oct 22, 2015

EF will automatically wire up the references between entities that have been queried for

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error when using FromSql and include ("The ...
Message: The Include operation is not supported when calling a stored procedure. Source: Microsoft.EntityFrameworkCore.Relational. enter image ...
Read more >
Support "Include" when using stored procedure in FromSql
Besides that, we can make Include() work when composable SQL (i.e. SELECT) statements are passed to FromSql() by layering any necessary filters ...
Read more >
Working with Stored Procedure in Entity Framework Core
Here, you will learn how to execute the database stored procedure in Entity Framework Core. EF Core provides the following methods to execute...
Read more >
SQL Queries - EF Core
You can use FromSql to begin a LINQ query based on a SQL query: ... SQL queries can be used to execute a...
Read more >
How to Execute Stored Procedures With EF Core 7
In this article, we are going to ;earn how to execute stored procedures in Entity Framework Core 7 with different examples.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found