[英]Spatial type Entity Framework Core SQL Server
I need to work with georaphic distances. 我需要处理短程距离。
I got 我有
public class Clinic
{
...
public double Latitude
public double Longitud
...
}
This clinics are on a SQL Server DB. 该诊所位于SQL Server数据库上。 I need to return them ordered by distance to a certain point.
我需要将它们按距离排序返回到特定点。 For what I read, Entity Framework Core has no support for DbGeography or something of that sort as to represent the SQL Server type:
就我所读的内容而言,Entity Framework Core不支持DbGeography或某种代表SQL Server类型的支持:
geography
I added to the database a column with the calculated geography point of each clinic 我在数据库中添加了一个列,其中包含每个诊所的已计算地理位置
UPDATE Clinics SET Geo = geography::Point(Clinics.Latitude, Clinics.Longitud,4326)
OK, know i need to return them ordered. 好吧,知道我需要将它们退还给您。 SQL supports this by querying
SQL通过查询来支持
DECLARE @p geography;
SET @p = geography::Point(41,2,4326);
SELECT * FROM Clinics c ORDER BY @p.STDistance(c.Geo);
and Entity Framework Core supports querying with a raw SQL. Entity Framework Core支持使用原始SQL查询。
context.Clinics.FromSql("..query..")
Thing is, as the documentation of the FromSql says, if the data you are returning doesn't exactly match the model it crashes. 就像FromSql的文档所述,如果返回的数据与模型不完全匹配,则会崩溃。 As in C# I cant have a DbGeography that represents Geo I can't figure out how to resolve this.
像在C#中一样,我无法使用表示Geo的DbGeography,所以我不知道如何解决此问题。
EDIT 1: 编辑1:
Got it to partially work with the following: 可以部分使用以下内容:
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);
So now I get all the attributes of Clinics and it works! 因此,现在我掌握了Clinics的所有属性,并且可以正常工作! Thing is, clinics has a related class Amenities
问题是,诊所有相关的设施
public class Clinic
{
...
public double Latitude
public double Longitud
public Amenity amenity
...
}
So when I run a query I need to include the amenities by linq 因此,当我运行查询时,我需要包含linq的便利设施
query = query.Include(c => c.ClinicAmenities).ThenInclude(ca => ca.Amenity);
Dies because Orderby from the raw sql comes before the include I believe 之所以死,是因为我相信原始SQL的Orderby会在包含之前出现
You didn't specify what error you get. 您没有指定遇到什么错误。
It seems to me you get error "The Include operation is not supported when calling a stored procedure." 在我看来,您会收到错误消息“调用存储过程时不支持Include操作”。
What you could do is execute two queries. 您可以执行两个查询。
Something like this 像这样
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.