1

My model contains the following entity:

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

I have added the following partial class to the business layer:

public partial class Address
{
  // The geography spatial data type, geography, represents data in a round-earth coordinate system and has a default SRID value of 4326.
  private static readonly int SpatialReferenceIdentifier = 4326;

  public void SetPoint(double latitude, double longitude)
  {
    this.SpatialLocation = SqlGeography.Point(latitude, longitude, SpatialReferenceIdentifier);
  }

  public double Latitude
  {
    get
    {
      return this.SpatialLocation == null ? 0 : this.SpatialLocation.Lat.Value;
    }
  }

  public double Longitude
  {
    get
    {
      return this.SpatialLocation == null ? 0 : this.SpatialLocation.Long.Value;
    }
  }
}

I have added the Nuget package Microsoft.SqlServer.Types to the businesslayer project.

My console application contains the following code:

SqlServerTypes.Utilities.LoadNativeAssemblies(AppDomain.CurrentDomain.BaseDirectory);

Address address = new Address();
address.SetPoint(40, 50);
address.Save();
Console.WriteLine("Latitude: {0}", address.Latitude.ToString());
Console.WriteLine("Longitude: {0}", address.Longitude.ToString());

Address address2 = Address.LoadByEntityKey(address.EntityKey);
Console.WriteLine("Latitude: {0}", address2.Latitude.ToString());
Console.WriteLine("Longitude: {0}", address2.Longitude.ToString());

The output of this console application is: 40 50 0 0

The SpatialLocation is not null in the database. However, the following line of code in the produced ReadRecord method returns null:

this._spatialLocation = ((Microsoft.SqlServer.Types.SqlGeography)(persistence.GetReaderValueObject(reader, "Address_SpatialLocation", default(Microsoft.SqlServer.Types.SqlGeography), typeof(Microsoft.SqlServer.Types.SqlGeography), CodeFluent.Runtime.PersistenceSerializationMode.Default)));

I have added assemblyPaths to the project node but that does not solve the problem.

<cf:project assemblyPaths="..\LIB\Microsoft.SqlServer.Types.dll" ...>
  1. Why does CodeFluent not load the SpatialLocation in the ReadRecord method?

  2. What does the assemblyPaths attribute do? Does CodeFluent need this? Why?

  3. Do I need to call the LoadNativeAssemblies in my console application?

Willem
  • 111
  • 4

1 Answers1

0
  1. Why does CodeFluent not load the SpatialLocation in the ReadRecord method?

You should check the first chance exception in the output window, because the ReadRecord method swallows exceptions. There are many reasons for not being able to read the value. In your case, I think this is a mismatch of version that you can fixed using a bindingRedirect:

<configuration> 
  <runtime> 
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
      <dependentAssembly> 
        <assemblyIdentity name="Microsoft.SqlServer.Types" culture="neutral" publicKeyToken="89845dcd8080cc91"/>         
        <bindingRedirect oldVersion="10.0.0.0" newVersion="14.0.0.0" /> 
      </dependentAssembly> 
    </assemblyBinding> 
  </runtime> 
</configuration> 

Don't hesitate to post the content of the output window, if you think there is another reason.

  1. What does the assemblyPaths attribute do? Does CodeFluent need this? Why?

CodeFluent Entities resolves the typeName at build time. This is required to generate the right ReadXXXValue. Thus, if you are using a dotnet type which is not in the GAC, you need to add a reference to generate the right code.

  1. Do I need to call the LoadNativeAssemblies in my console application?

You shouldn't not need to call the LoadNativeAssemblies in a console app. However, you must check the dll Microsoft.SqlServer.Types in the bin directory.

BTW, there was a blog post on http://blog.codefluententities.com but the site seems to be down. You can find an offline version of this post (maybe not up to date) on OneDrive: https://1drv.ms/w/s!AkYmf2ZXtwX4gfA27FKegjJgz5PB8g

meziantou
  • 20,589
  • 7
  • 64
  • 83
  • The bindingRedirect solved the problem. I had to redirect to new version 14.0.0.0. It seems that CodeFluent does not need the assemblyPaths attribute in my project, because without this attribute my applications works fine too. – Willem Feb 08 '18 at 19:04