{"id":58,"date":"2012-05-05T22:54:15","date_gmt":"2012-05-05T20:54:15","guid":{"rendered":"http:\/\/www.spatially-oriented.com\/blog\/?p=58"},"modified":"2012-05-05T22:56:28","modified_gmt":"2012-05-05T20:56:28","slug":"amazing-entityframework-4-spatial","status":"publish","type":"post","link":"https:\/\/www.spatially-oriented.com\/blog\/2012\/05\/05\/amazing-entityframework-4-spatial\/","title":{"rendered":"Amazing&#8230; EntityFramework 4 + Spatial"},"content":{"rendered":"<p>Well, to be fair, I did not see this one coming. I can&#8217;t believe how easy this stuff worked.<\/p>\n<p>So, I am dabling with new things lately. I haven&#8217;t been taking close watch to new technologies so I&#8217;m trying to get myself back up with the program. As my main focus (and joy) lies with maps nowadays, I wanted to see if and how Entity Framework would work with a Spatial Database. Now I am not going to trying big things on my first ride, so I used my SQL Server 2010 Express, installed the new Visual Studio 11 beta web and went cracking.<\/p>\n<p><!--more--><\/p>\n<p>I already had this table in my database filled with a lot of adresses of the Netherlands, so I decided to use that to play with. After adding EF4 to my project, I first created a new model conveniently called &#8220;test&#8221;. I tested this with and older version before and found out it didn&#8217;t have Spatial types. Now, clearly, they do. \ud83d\ude42<\/p>\n<p>So then I proceded with reverse enginering my database. (it just contains one table with adresses) VS11 had no problems doing that, so I was up and running in no time.<\/p>\n<p>Then, about 10 minutes after googling &#8220;EntityFramework for dummies&#8221; I had a working demo with live data. I queried my database for all adresses in my street. No problem!<\/p>\n<p>Then, on to Spatial! (Because, well it aint fun if it ain spatial!) The Geometry Type is in fact a special SQL Server type, and it grants you functions als getting a WKT, calculating a distance between geometries, disjoints. I haven&#8217;t spend too much time checking out every option, and there is bound to be a factsheet around <a href=\"http:\/\/www.google.com\" target=\"_blank\">there somewhere<\/a>.<\/p>\n<p>So a short piece of testcode where I pull the adresses that are within 50m from my house.<\/p>\n<p><code lang=\"4cs\">IList<string> adressen = new List<string>();<br \/>\nusing (var db = new Model1Container())<br \/>\n{<br \/>\n    var homeSweetHome = DbGeometry.FromText(<br \/>\n                               \"POINT (84366.76 454659.73)\", 28992);<br \/>\n    var adresQuery = from p in db.adres<br \/>\n                     where p.geometry.Distance(homeSweetHome) < 50\n                     select p;\n\n    foreach (var adres in adresQuery)\n    {\n        string adresje = string.Format(\"{0} {1}, {2}\", \n                     adres.openbareruimtenaam, \n                     adres.huisnummer + adres.huisletter,\n                     adres.gemeentenaam);\n        adressen.Add(adresje);\n   }\n}<\/code><\/p>\n<p>Safe to say, this worked. Note that Model1Container is my autogenerated code by EF4. DbGeometry can be found in System.Data.Spatial so it appears to be a non-SqlServer specific thing. Also, this is a query that's being run on a database with 8,5 milion adresses. This spatial query took only a few ms of time. In retrospect, this database is running on my local machine, with only me using the database. So I can't tell how it will handle big loads. (yet?) All I can say in retrospect... damn... that was easy.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Well, to be fair, I did not see this one coming. I can&#8217;t believe how easy this stuff worked. So, I am dabling with new things lately. I haven&#8217;t been taking close watch to new technologies so I&#8217;m trying to get myself back up with the program. As my main focus (and joy) lies with&hellip;<\/p>\n<p class=\"read-more\"><a class=\"readmore-btn\" href=\"https:\/\/www.spatially-oriented.com\/blog\/2012\/05\/05\/amazing-entityframework-4-spatial\/\">Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[5,4],"tags":[31,32],"class_list":["post-58","post","type-post","status-publish","format-standard","hentry","category-gis","category-tech","tag-entity-framework","tag-sqlserver"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2n8yP-W","_links":{"self":[{"href":"https:\/\/www.spatially-oriented.com\/blog\/wp-json\/wp\/v2\/posts\/58","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.spatially-oriented.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.spatially-oriented.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.spatially-oriented.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.spatially-oriented.com\/blog\/wp-json\/wp\/v2\/comments?post=58"}],"version-history":[{"count":12,"href":"https:\/\/www.spatially-oriented.com\/blog\/wp-json\/wp\/v2\/posts\/58\/revisions"}],"predecessor-version":[{"id":71,"href":"https:\/\/www.spatially-oriented.com\/blog\/wp-json\/wp\/v2\/posts\/58\/revisions\/71"}],"wp:attachment":[{"href":"https:\/\/www.spatially-oriented.com\/blog\/wp-json\/wp\/v2\/media?parent=58"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.spatially-oriented.com\/blog\/wp-json\/wp\/v2\/categories?post=58"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.spatially-oriented.com\/blog\/wp-json\/wp\/v2\/tags?post=58"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}