<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Spatial on Andrew Bancroft</title>
    <link>https://www.andrewcbancroft.com/tags/spatial/</link>
    <description>Recent content about iOS development with Swift in Spatial  from Andrew Bancroft.</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <lastBuildDate>Mon, 27 Mar 2017 16:38:33 +0000</lastBuildDate>
    
        <atom:link href="https://www.andrewcbancroft.com/tags/spatial/index.xml" rel="self" type="application/rss+xml" />
    
    
    <item>
      <title>Solving ‘Spatial types and functions are not available’ with Entity Framework</title>
      <link>https://www.andrewcbancroft.com/2017/03/27/solving-spatial-types-and-functions-are-not-available-with-entity-framework/</link>
      <pubDate>Mon, 27 Mar 2017 16:38:33 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/2017/03/27/solving-spatial-types-and-functions-are-not-available-with-entity-framework/</guid>
      <description>&lt;p&gt;Using SQL Server&amp;rsquo;s Geospatial features with Entity Framework is awesome. I was crusing along just fine with using &lt;code&gt;DbGeography&lt;/code&gt; for an ASP.Net application I&amp;rsquo;m working on… Right up until I deployed to the server. Yep. It worked on &lt;em&gt;my&lt;/em&gt; machine, but alas, I was hitting a runtime exception on my test server.&lt;/p&gt;
&lt;p&gt;This was the exception:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Spatial types and functions are not available for this provider because the assembly ‘Microsoft.SqlServer.Types&amp;rsquo; version 10 or higher could not be found.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Luckily, I ran across a &lt;a href=&#34;http://stackoverflow.com/questions/13174197/microsoft-sqlserver-types-version-10-or-higher-could-not-be-found-on-azure&#34;&gt;Stack Overflow answer&lt;/a&gt; that &lt;em&gt;almost&lt;/em&gt; helped me get all the way there in solving this exception. It was actually &lt;a href=&#34;http://stackoverflow.com/a/40166192/3198996&#34;&gt;the second-most up-voted answer&lt;/a&gt; that helped me the most. You&amp;rsquo;re welcome to reference these if you want, but since the info is scattered between the question and the two answers, I&amp;rsquo;m assembling it here for your convenience.&lt;/p&gt;
&lt;h1 id=&#34;fixing-8216spatial-types-and-functions-are-not-available&#34;&gt;Fixing ‘Spatial types and functions are not available&#39;&lt;/h1&gt;
&lt;h2 id=&#34;1--install-the-microsoftsqlservertypes-package-from-nuget&#34;&gt;1 – Install the Microsoft.SqlServer.Types package from NuGet&lt;/h2&gt;
&lt;p&gt;You can install it using the NuGet Package Manager UI, or from the command line:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/03/NuGetPM_MSSqlServerTypes1.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/03/NuGetPM_MSSqlServerTypes1.png&#34; alt=&#34;NuGetPM_MSSqlServerTypes&#34; width=&#34;985&#34; height=&#34;270&#34; class=&#34;alignnone size-full wp-image-13100&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/03/NuGetPM_MSSqlServerTypes1.png 985w, https://www.andrewcbancroft.com/wp-content/uploads/2017/03/NuGetPM_MSSqlServerTypes1-300x82.png 300w&#34; sizes=&#34;(max-width: 985px) 100vw, 985px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;or&lt;/p&gt;
&lt;p&gt;&lt;code&gt;&amp;gt; Install-Package Microsoft.SqlServer.Types&lt;/code&gt;&lt;/p&gt;
&lt;h2 id=&#34;2--follow-readmehtm-instructions&#34;&gt;2 – Follow readme.htm instructions&lt;/h2&gt;
&lt;p&gt;After you install the NuGet package, a readme.htm file is opened and displayed to you. If you don&amp;rsquo;t see it for some reason, a new SqlServerTypes folder was added to your project. You can expand it and double-click readme.htm to open it.&lt;/p&gt;
&lt;p&gt;Follow the instructions within.&lt;/p&gt;
&lt;p&gt;Depending on the type of app you&amp;rsquo;re deploying, you need to perform different steps. I happened to be deploying an ASP.Net MVC app, so I followed the instructions for adding a line of code to Global.asax.cs. Your situation may be different, but the Readme instructions are clear on what to do.&lt;/p&gt;
&lt;h3 id=&#34;aspnet-web-_applications_&#34;&gt;ASP.Net Web &lt;em&gt;Applications&lt;/em&gt;&lt;/h3&gt;
&lt;p&gt;Open Global.asax.cs and add the following to the list of registrations in &lt;code&gt;Application_Start()&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-c#&#34; data-lang=&#34;c#&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;protected&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;void&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Application_Start&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;3&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Enables use of spatial data types&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;4&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;n&#34;&gt;SqlServerTypes&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Utilities&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;LoadNativeAssemblies&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Server&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;MapPath&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;~/bin&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;));&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;5&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;6&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Other registrations...&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;7&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&#34;aspnet-_websites_&#34;&gt;Asp.Net &lt;em&gt;Websites&lt;/em&gt;&lt;/h3&gt;
&lt;p&gt;Open Default.aspx.cs and add&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-c#&#34; data-lang=&#34;c#&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 1&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;partial&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;_Default&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;System&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Web&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;UI&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Page&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 2&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 3&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;static&lt;/span&gt; &lt;span class=&#34;kt&#34;&gt;bool&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;_isSqlTypesLoaded&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;false&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 4&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 5&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;_Default&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 6&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 7&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;(!&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;_isSqlTypesLoaded&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 8&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 9&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;n&#34;&gt;SqlServerTypes&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Utilities&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;LoadNativeAssemblies&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Server&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;MapPath&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;~&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;));&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;10&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;n&#34;&gt;_isSqlTypesLoaded&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;true&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;11&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;12&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;13&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;14&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&#34;desktop-applications&#34;&gt;Desktop Applications&lt;/h3&gt;
&lt;p&gt;Add the following before any spatial operations are performed.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-c#&#34; data-lang=&#34;c#&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;SqlServerTypes&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Utilities&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;LoadNativeAssemblies&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;AppDomain&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;CurrentDomain&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;BaseDirectory&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&#34;3--tell-entity-framework-which-version-of-the-assembly-to-use&#34;&gt;3 – Tell Entity Framework which version of the assembly to use&lt;/h3&gt;
&lt;p&gt;Most of the explanations of how to solve the &amp;ldquo;‘Microsoft.SqlServer.Types&amp;rsquo; version 10 or higher” error stopped at step 2.&lt;/p&gt;
&lt;p&gt;After performing the steps that readme.htm outlined, I re-deployed the app, and things were still broken. Same exception. What gives??&lt;/p&gt;
&lt;p&gt;This is where that &lt;a href=&#34;http://stackoverflow.com/a/40166192/3198996&#34;&gt;second-most-popular answer&lt;/a&gt; came into play. It gave me the clue I needed.&lt;/p&gt;
&lt;p&gt;Back in Global.asax.cs (or wherever you performed the steps for #2 above), add the following”&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;    SqlProviderServices.SqlServerTypesAssemblyName =
    &amp;#34;Microsoft.SqlServer.Types, Version=14.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91&amp;#34;;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Depending on which version of the NuGet package you installed, you need to adjust the Version number from 14.0.0.0 to the correct version, as appropriate. I installed the latest, which at the time is version 14.x, so that&amp;rsquo;s what I put in.&lt;/p&gt;
&lt;h4 id=&#34;-the-sqlservertypesassemblyname-property-doesnt-exist-&#34;&gt;??? The SqlServerTypesAssemblyName property doesn&amp;rsquo;t exist! ???&lt;/h4&gt;
&lt;p&gt;In my case, I ran into another small hurdle. When I tried to set the &lt;code&gt;SqlServerTypesAssemblyName&lt;/code&gt; property, it didn&amp;rsquo;t exist!&lt;/p&gt;
&lt;p&gt;Upon further inspection, I discovered that I only had Entity Framework version 6.1.**** installed. I updated to 6.1.&lt;strong&gt;3&lt;/strong&gt;, and the property lit up. I set it appropriately, redeployed the app, and it magically worked.&lt;/p&gt;
&lt;p&gt;Here&amp;rsquo;s hoping that this helps bring together several pieces of information to get you going with using SQL Server Geospatial data types with Entity Framework!&lt;/p&gt;
</description>
    </item>
    
  </channel>
</rss>