Search code examples
sql-serverentity-frameworklinqgeospatialsqlgeography

Entity Framework 6 and Geography data STContains


I've included Microsoft.SqlServer.Types to enable the geography and geometry types in Entity Framework, but I don't see any functions equivalent to STContains().

I need to make a query to retrieve the geography that contains a point

In SQL I wrote like this:

SELECT adm1code, adm1name 
FROM Adm2GeoBoundaries
WHERE Coords.STContains(geography::Parse('POINT(-121.703796 46.893985)'));

in LINQ I expect to have something like

using (GeoEntities db = new GeoEntities ())
{
    DbGeography location = DbGeography.FromText("POINT(-121.703796 46.893985)");
    var admin = from a in db.Adm2GeoBoundaries
                where a.Coords.STContains(location)
                select a;
}

but a.Coords.STContains(location) throws an error

STContains method doesn't exist


Solution

  • According to the source code for EF6 Source Code, the STContains seems to be implemented as Contains in EF6.

    https://github.com/aspnet/EntityFramework6/blob/master/src/EntityFramework.SqlServer/SqlSpatialServices.cs

    enter image description here

    Looking at the SqlTypesAssembly.cs you should be able see it should invoke the STContains method.

    enter image description here