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?

simbolo
  • 7,279
  • 6
  • 56
  • 96

1 Answers1

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());
BenCr
  • 5,991
  • 5
  • 44
  • 68
Jon Bellamy
  • 3,333
  • 20
  • 23
  • 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 '14 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. – Jon Bellamy Apr 21 '14 at 08: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 http://stackoverflow.com/a/29200641/472149 for an example. – Serge Belov Mar 22 '15 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. – Jon Bellamy Mar 23 '15 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 '16 at 17:26
  • It's okay, I worked it out, the example had a slight error, DbGeography has the `AsText()` method. – BenCr Mar 14 '16 at 17:29
  • What is the MakeValid() you mentioned in another article https://stackoverflow.com/a/16087110/1830909 – QMaster Jul 19 '18 at 12:28
  • @QMaster MakeValid() attempts to correct a Geometry/Geography instance which is defined as Invalid based on SQL implementation of geometric object standards and can be found using STIsValid() or IsValidDetailed(). A warning however - MakeValid will cause a small shift in the object's coordinates when used so I urge caution if this would cause you an issue. – Jon Bellamy Jul 19 '18 at 13:56
  • Thank you so much, I'll check it. – QMaster Jul 20 '18 at 15:46