EF Core 2.0 FromSql load related entities

.net-core c# entity-framework-core sql-server

Question

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;
    }
1
2
9/19/2017 3:07:04 PM

Popular Answer

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

2
9/19/2017 5:03:29 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow