我的模型包含以下实体:
<Address>
<Id />
<SpatialLocation cfps:dataType="geography" typeName="Microsoft.SqlServer.Types.SqlGeography, Microsoft.SqlServer.Types"/>
</Address>我在业务层中添加了以下部分类:
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;
}
}
}我已经将Nuget包Microsoft.SqlServer.Types添加到业务层项目中。
我的控制台应用程序包含以下代码:
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());此控制台应用程序的输出为: 40 50 0 0
数据库中的SpatialLocation不是空的。但是,生成的ReadRecord方法中的以下代码行返回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)));我已经将assemblyPaths添加到项目节点,但这并不能解决问题。
<cf:project assemblyPaths="..\LIB\Microsoft.SqlServer.Types.dll" ...>发布于 2018-02-07 15:25:19
您应该在输出窗口中检查第一次机会异常,因为ReadRecord方法会吞噬异常。有很多原因不能理解这个值。在您的情况下,我认为这是一个不匹配的版本,您可以使用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> 如果您认为还有其他原因,请不要犹豫地发布输出窗口的内容。
CodeFluent实体在构建时解析typeName。这是生成正确的ReadXXXValue所必需的。因此,如果使用不在GAC中的dotnet类型,则需要添加引用以生成正确的代码。
您不需要在控制台应用程序中调用LoadNativeAssemblies。但是,必须检查bin目录中的dll Microsoft.SqlServer.Types。
顺便说一句,http://blog.codefluententities.com上有一篇博客文章,但这个网站似乎已经瘫痪了。您可以在OneDrive:https://1drv.ms/w/s!AkYmf2ZXtwX4gfA27FKegjJgz5PB8g上找到这篇文章的脱机版本(可能不是最新的)。
https://stackoverflow.com/questions/48662156
复制相似问题