Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework: SqlGeography vs DbGeography

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?

like image 739
simbolo Avatar asked Apr 20 '14 19:04

simbolo


1 Answers

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());
like image 180
Jon Bellamy Avatar answered Oct 03 '22 17:10

Jon Bellamy