So here's the deal - I am currently using EF Core 3.1 and let's say I have an entity:
public class Entity
{
public int Id { get; set; }
public int AnotherEntityId { get; set; }
public virtual AnotherEntity AnotherEntity { get; set; }
}
When I access the DbSet<Entity> Entities
normal way, I include AnotherEntity like:
_context.Entities.Include(e => e.AnotherEntity)
and this works. Why wouldn't it, right? Then I go with:
_context.Entities.FromSqlRaw("SELECT * FROM Entities").Include(e => e.AnotherEntity)
and this also works. Both return me the same collection of objects joined with AnotherEntity. Then I use a stored procedure which consists of the same query SELECT * FROM Entities
named spGetEntities:
_context.Entities.FromSqlRaw("spGetEntities")
guess what? This also works. It gives me the same output but without joined AnotherEntity, obviously. However if I try to add the Include like this:
_context.Entities.FromSqlRaw("spGetEntities").Include(e => e.AnotherEntity)
I am getting:
FromSqlRaw or FromSqlInterpolated was called with non-composable SQL and with a query composing over it. Consider calling
AsEnumerable
after the FromSqlRaw or FromSqlInterpolated method to perform the composition on the client side.
Even though the output of _context.Entities.FromSqlRaw("SELECT * FROM Entities")
and _context.Entities.FromSqlRaw("spGetEntities")
is identical.
I couldn't find a proof that I can or I can not do this with EF Core 3.1 but if someone could give me any hint of possibility of this approach it would be nice.
Also if there is another way to get joined entities using stored procedure I would probably accept it as the solution of my issue.
Include
, not about how to useFromSqlRaw
. The accepted answer is all there is to it. – Fluttery