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" ...>
Why does CodeFluent not load the SpatialLocation in the ReadRecord method?
What does the assemblyPaths attribute do? Does CodeFluent need this? Why?
Do I need to call the LoadNativeAssemblies in my console application?