简体   繁体   中英

.NET 4.6 / Entity Framework : Spatial functions throw not supported

This might be long shot and I think I already have the answer, however I want to see if someone got this resolved in a different way. Using the Entity Framework btw.

Ok, I am getting polygons from a database for a map (boundaries) and want to reduce the amount of points directly in the call. Great, SqlServer actually has the spatial function "Reduce" built-in.

Here is my code:

data = (from c in context.Spatial_Zips
    where zips.Contains(c.regionname)
    select new Spatial_Zip() {
        ID = c.ID,
        regionname = c.regionname,
        pcnameimp = c.pcnameimp,
        geog = SqlSpatialFunctions.Reduce(c.geog, 100)
    }).ToList();    

Great, works all good until it actually hits the reduce function. mehmehmehm: It throws a Unsupported exception.

Well I dig'ed a little bit deeper and actually came across this reference:

http://referencesource.microsoft.com/#System.Data.Entity/System/Data/Objects/SqlClient/SqlSpatialFunctions.cs

Looks like it's not even implemented:

/// <summary>
        /// Proxy for the function SqlServer.REDUCE
        /// </summary>
        [EdmFunction("SqlServer", "REDUCE")]
        public static System.Data.Spatial.DbGeography Reduce(System.Data.Spatial.DbGeography geographyValue, System.Double? tolerance)
        {
            throw EntityUtil.NotSupported(System.Data.Entity.Strings.ELinq_EdmFunctionDirectCall);
        }

Now my question is does anyone know how to execute this statement in raw SQL using the Entity Framework?

I don't think there is any other option at this point, or is there?

There is Database instance as a property of DbContext. You can use Database class's SqlQuery method to issue raw SQL.

For example

var directQuery = context.Database.SqlQuery<Spatial_Zip>(sql, new object[] { }).ToList();

Take a look at www.asp.net for more info.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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