16

What is the difference, or intended purpose of these two object that represent the same Geography datatype in a Microsoft SQL Database?

System.Data.Entity.Spatial.DbGeography

and

Microsoft.SqlServer.Types.SqlGeography

They can't be cast between each other, but SqlGeography has additional command when it comes to creating points, polygons etc.

I'm thinking that System.Data.Entity is for use with Entity Framework only, whilst Microsoft.SqlServer is for use when using the SqlCommand directly instead?

1 Answer 1

14

You're right, in essence it is that simple. DbGeography is just a dumbed down version of SqlGeography, designed to work within the Entity Framework. The most popular methods of SqlGeography have been implemented within it but as you rightly point out, not all.

Whilst the two types cannot be directly cast between each other, the process of converting them is relatively simple in times where the additional functionality of SqlGeography is required.

For example:

SqlGeography geog1 = SqlGeography.STPolyFromText('<coords>', srid);
SqlGeography geog2;
DbGeography dbGeog;

// SqlGeography to DbGeography
dbGeog = DbGeography.FromText(geog1.ToString(), srid);
// DbGeography to SqlGeography
geog2 = SqlGeography.Parse(dbGeog.AsText());
9
  • That's a good workaround re: interchanging between the two types. Maybe in the future the types will become more compatible or feature full since SqlGeography as some useful functions.
    – simbolo
    Apr 20, 2014 at 23:31
  • @simbolo, it would but I would also get their reasons for not doing so, anything else can be used natively in SQL by mapping a Stored Procedure to EF, or by simply accessing via ADO. Apr 21, 2014 at 8:36
  • Converting between these types using text (WKT) is extremely inefficient, the conversion ideally should be based on the well-known binary format, please see stackoverflow.com/a/29200641/472149 for an example. Mar 22, 2015 at 22:34
  • @SergeBelov Defining the difference as "extremely inefficient" is perhaps a little harsh when we're talking single-digit milliseconds and when performance is platform dependent. Never-the-less, I agree that is is faster and have used the Binary methods myself for some time now. Thank you for taking the time to update the thread. Mar 23, 2015 at 16:18
  • Am I missing a reference somewhere or a using with some extension methods. AsText() doesn't exist on SqlGeography
    – BenCr
    Mar 14, 2016 at 17:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.