Home > SQL Server > CodeFluent Entities and the SqlGeography Type

CodeFluent Entities and the SqlGeography Type


Although there is not a geography type in CodeFluent Entities (as this is too specific to SQL Server 2008), you can still use the new SQL 2008 geography type today.

First you need to declare the target type in the CodeFluent Entities SQL Server Producer (with the ‘cfps:dataType’ attribute). Then, optionally, you can declare a computed companion property that will hold the data as the corresponding SQL Server 2008 .NET Type: SqlGeography.

It will work because these types can be transferred back and forth as arrays of bytes. Here is a sample model that does it:

Code:

<cf:project xmlns:cf="http://www.softfluent.com/codefluent/2005/1"
                   defaultNamespace="Test"
                   xmlns:cfps="http://www.softfluent.com/codefluent/producers.sqlserver/2005/1">

 <Address>
  <Id />
  <RawSpatialLocation cfps:dataType="geography" typeName="byte[]" maxLength="-1" />
  <SpatialLocation typeName="Microsoft.SqlServer.Types.SqlGeography" persistent="false">
  <cf:rule typeName="OnGet" />
  <cf:rule typeName="OnAfterSet" />
  <cf:snippet name="SpatialLocation">

 private void OnGetSpatialLocation()
 {
  _spatialLocation = new Microsoft.SqlServer.Types.SqlGeography();
  using (System.IO.MemoryStream stream = new System.IO.MemoryStream(RawSpatialLocation))
  {
   using (System.IO.BinaryReader reader = new System.IO.BinaryReader(stream))
   {
    _spatialLocation.Read(reader);
   }
  }
 }

 private void OnAfterSetSpatialLocation(Microsoft.SqlServer.Types.SqlGeography spatialLocation)
 {
  using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
  {
   using (System.IO.BinaryWriter writer = new System.IO.BinaryWriter(stream))
   {
    _spatialLocation.Write(writer);
    _rawSpatialLocation = stream.ToArray();
   }
  }
 }
  </SpatialLocation>
 </Address>
</cf:project>

NOTE1: make sure Microsoft.SqlServer.Types.dll is referenced for the project to compile.

NOTE2: all this is valid for the SqlGeography, SqlGeometry and SqlHierarchyId types.

The next version of CodeFluent Entities will allow you to directly declare this:

Introduced with the latest CodeFluent Entities build (B31218), you can now do this:

Code:

<cf:project xmlns:cf="http://www.softfluent.com/codefluent/2005/1"
                  defaultNamespace="Test"
                  xmlns:cfps="http://www.softfluent.com/codefluent/producers.sqlserver/2005/1"
                  assemblyPaths="c:\temp\Microsoft.SqlServer.Types.dll"> <!-- adapt this to your machine -->

 <Address>
  <Id />
  <SpatialLocation cfps:dataType="geography" typeName="Microsoft.SqlServer.Types.SqlGeography, Microsoft.SqlServer.Types" />
</Address>
</cf:project>

CodeFluent Entities R&D Team

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s