首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CodeFluent返回地理列的null

CodeFluent返回地理列的null
EN

Stack Overflow用户
提问于 2018-02-07 11:00:34
回答 1查看 48关注 0票数 1

我的模型包含以下实体:

代码语言:javascript
复制
<Address>
  <Id />   
  <SpatialLocation cfps:dataType="geography" typeName="Microsoft.SqlServer.Types.SqlGeography, Microsoft.SqlServer.Types"/>
</Address>

我在业务层中添加了以下部分类:

代码语言:javascript
复制
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添加到业务层项目中。

我的控制台应用程序包含以下代码:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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添加到项目节点,但这并不能解决问题。

代码语言:javascript
复制
<cf:project assemblyPaths="..\LIB\Microsoft.SqlServer.Types.dll" ...>
  1. 为什么CodeFluent不在ReadRecord方法中加载SpatialLocation?
  2. assemblyPaths属性是做什么的?CodeFluent需要这个吗?为什么?
  3. 我需要在我的控制台应用程序中调用LoadNativeAssemblies吗?
EN

回答 1

Stack Overflow用户

发布于 2018-02-07 15:25:19

  1. 为什么CodeFluent不在ReadRecord方法中加载SpatialLocation?

您应该在输出窗口中检查第一次机会异常,因为ReadRecord方法会吞噬异常。有很多原因不能理解这个值。在您的情况下,我认为这是一个不匹配的版本,您可以使用bindingRedirect修复。

代码语言:javascript
复制
<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> 

如果您认为还有其他原因,请不要犹豫地发布输出窗口的内容。

  1. assemblyPaths属性是做什么的?CodeFluent需要这个吗?为什么?

CodeFluent实体在构建时解析typeName。这是生成正确的ReadXXXValue所必需的。因此,如果使用不在GAC中的dotnet类型,则需要添加引用以生成正确的代码。

  1. 我需要在我的控制台应用程序中调用LoadNativeAssemblies吗?

您不需要在控制台应用程序中调用LoadNativeAssemblies。但是,必须检查bin目录中的dll Microsoft.SqlServer.Types。

顺便说一句,http://blog.codefluententities.com上有一篇博客文章,但这个网站似乎已经瘫痪了。您可以在OneDrive:https://1drv.ms/w/s!AkYmf2ZXtwX4gfA27FKegjJgz5PB8g上找到这篇文章的脱机版本(可能不是最新的)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48662156

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档