简体   繁体   English

EF Core 2.0 FromSql负载相关实体

[英]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. 我已经存储了加载事件及其位置的过程,但是当我尝试使用FromSql方法从我的Repository类中调用它时,我仅获得事件实体的列表,但不包括位置。 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? 我尝试将Include方法添加到查询中,但出现异常,提示调用存储过程时无法使用Inclued ...有任何方法可以这样做,或者我必须手动映射所有属性?

My SP: 我的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 和我的C#代码

    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;
    }

这是相关的问题https://github.com/aspnet/EntityFrameworkCore/issues/3502如果使用sql函数而不是存储过程,它将与Include一起使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM