Spatial type Entity Framework Core SQL Server

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

Question

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. 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:

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

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.

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. As in C# I cant have a DbGeography that represents Geo I can't figure out how to resolve this.

EDIT 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! 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

query = query.Include(c => c.ClinicAmenities).ThenInclude(ca => ca.Amenity);

Dies because Orderby from the raw sql comes before the include I believe

1
8
2/9/2018 9:02:43 PM

Popular Answer

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."

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();
1
2/7/2018 2:40:34 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