[英]Entity Framework not getting Spatial type data in result from Sql Server stored procedure
[英]Spatial type Entity Framework Core SQL Server
我需要处理短程距离。
我有
public class Clinic
{
...
public double Latitude
public double Longitud
...
}
该诊所位于SQL Server数据库上。 我需要将它们按距离排序返回到特定点。 就我所读的内容而言,Entity Framework Core不支持DbGeography或某种代表SQL Server类型的支持:
geography
我在数据库中添加了一个列,其中包含每个诊所的已计算地理位置
UPDATE Clinics SET Geo = geography::Point(Clinics.Latitude, Clinics.Longitud,4326)
好吧,知道我需要将它们退还给您。 SQL通过查询来支持
DECLARE @p geography;
SET @p = geography::Point(41,2,4326);
SELECT * FROM Clinics c ORDER BY @p.STDistance(c.Geo);
Entity Framework Core支持使用原始SQL查询。
context.Clinics.FromSql("..query..")
就像FromSql的文档所述,如果返回的数据与模型不完全匹配,则会崩溃。 像在C#中一样,我无法使用表示Geo的DbGeography,所以我不知道如何解决此问题。
编辑1:
可以部分使用以下内容:
string rawSQL = @"
DECLARE @p geography;
SET @p = geography::Point(41,2,4326);
SELECT c.Id, c.Name, c.Price, c.Quote, c.QuoteAuthor, c.QuoteSource, c.Description, c.Image, c.Latitude, c.Longitude, c.Category, c.CenterDistance, c.NumReviews, c.Stars
FROM Clinics c
ORDER BY @p.STDistance(c.Geo); ";
query = query.FromSql(rawSQL);
因此,现在我掌握了Clinics的所有属性,并且可以正常工作! 问题是,诊所有相关的设施
public class Clinic
{
...
public double Latitude
public double Longitud
public Amenity amenity
...
}
因此,当我运行查询时,我需要包含linq的便利设施
query = query.Include(c => c.ClinicAmenities).ThenInclude(ca => ca.Amenity);
之所以死,是因为我相信原始SQL的Orderby会在包含之前出现
您没有指定遇到什么错误。
在我看来,您会收到错误消息“调用存储过程时不支持Include操作”。
您可以执行两个查询。
像这样
dbContext.Amenities.Load();
string rawSQL = @"
DECLARE @p geography;
SET @p = geography::Point(41,2,4326);
SELECT c.Id, c.Name, c.Price, c.Quote, c.QuoteAuthor
FROM Clinics c
ORDER BY @p.STDistance(c.Geo)";
// EF will automatically wire up the references between entities
// that have been queried for
var clinicsWithAmenities = dbContext.Clinics.FromSql(rawSQL).ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.