Kryštof Macek
Kryštof Macek

Reputation: 31

EF Core 2.0 FromSql load related entities

I have stored procedure which loads events and their locations but when i try to call it from my Repository class using FromSql method I only get list of Event entities but locations aren't included. I tried to add Include method to my query but I got an exception that says Inclued can't be used when calling stored procedure... Is there any way to do it or i have to manually map all properties?

My SP:

ALTER PROCEDURE [dbo].[GetEventsByDistance]
@Latitude float,   
@Longitude float,  
@Radius int
AS   
with MyCte as
(
    select dbo.fnCalcDistanceKM(l.Latitude, l.Longitude, @Latitude, @Longitude) as distance, a.*, l.*
    from dbo.Events a 
    JOIN Locations as l ON l.IdLocation = a.LocationId
)
SELECT
*
From MyCte
where distance < @Radius
RETURN 

and my C# code

    public async Task<List<Event>> GetEventsByDistance(string latitude, string longitude, int radiusInKm)
    {
        var events = new List<Akce>();
        using (var context = _factory.CreateDbContext(null))
        {
            events = await context.Akce.FromSql($"{Constants.DB_PROC_GetEventsByDistance} {latitude}, {longitude}, {radiusInKm}").ToListAsync();
        }
        return events;
    }

Upvotes: 2

Views: 2350

Answers (1)

Jaroslav Surala
Jaroslav Surala

Reputation: 93

Here is related issue https://github.com/aspnet/EntityFrameworkCore/issues/3502 If you use sql function instead of stored procedure it will work with Include

Upvotes: 2

Related Questions