Entity Framework: SqlGeography vs DbGeography
Asked Answered
B

1

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?

Brainstorm answered 20/4, 2014 at 19:45 Comment(0)
W
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());
Wipe answered 20/4, 2014 at 20:7 Comment(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.Brainstorm
@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.Wipe
Converting between these types using text (WKT) is extremely inefficient, the conversion ideally should be based on the well-known binary format, please see https://mcmap.net/q/390932/-sql-geography-to-dbgeography for an example.Sephira
@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.Wipe
Am I missing a reference somewhere or a using with some extension methods. AsText() doesn't exist on SqlGeographyHiss
It's okay, I worked it out, the example had a slight error, DbGeography has the AsText() method.Hiss
What is the MakeValid() you mentioned in another article https://mcmap.net/q/390932/-sql-geography-to-dbgeographyAdlee
@Adlee 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.Wipe
Thank you so much, I'll check it.Adlee

© 2022 - 2024 — McMap. All rights reserved.