我正在将NHibernate 3引入ASP.NET MVC Web应用程序的数据访问层。
在SQL Server 2008 R2数据库中,geography
数据类型用于在映射到应用程序中的实体的表中存储纬度/经度坐标(调用此EntityA
)。
我已经设置了流畅的映射,以便成功地将数据类型映射到类型为EntityA
的{{1}}上的属性,并使用自定义映射约定将其映射到派生的GisSharpBlog.NetTopologySuite.Geometries.Point
。< / p>
以上所有内容似乎都正常工作,但仍有一些存储过程仍在使用,希望一个参数的类型为MsSql2008GeographyType
。我已经将存储过程映射为命名查询,但我似乎无法弄清楚我应该为类型geography
的参数传递什么类型。查询调用发生的方法采用geography
纬度和double
经度。在旧的sproc调用中,映射是用
double
运行SQL事件探查器,执行以下命令
private static SqlGeography GetPoint(double latitude, double longitude)
{
var geographyBuilder = new SqlGeographyBuilder();
geographyBuilder.SetSrid(4326);
geographyBuilder.BeginGeography(OpenGisGeographyType.Point);
geographyBuilder.BeginFigure(latitude, longitude);
geographyBuilder.EndFigure();
geographyBuilder.EndGeography();
return geographyBuilder.ConstructedGeography;
}
var point = GetPoint(latitude, longitude);
command.Parameters.Add(
new SqlParameter("@Location", point) { UdtTypeName = "Geography" });
当我尝试使用以下
进行映射时declare @p3 sys.geography
set @p3=convert(sys.geography,0xE6100000010CAD4D637B2DBA49400BEE77280AB404C0)
exec my-sproc-name @Location=@p3
我得到一个带有var query = Session.GetNamedQuery("my-sproc-name");
var point =
new GisSharpBlog.NetTopologySuite.Geometries.Point(longitude, latitude);
query.SetParameter("Location", point);
[SqlException(0x80131904):将数据类型varbinary转换为的错误 地理。将数据类型varbinary转换为地理位置时出错。] System.Data.SqlClient.SqlConnection.OnError(SqlException异常, Boolean breakConnection)+2073502
System.Data.SqlClient.SqlInternalConnection.OnError(SQLEXCEPTION exception,Boolean breakConnection)+5064460
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()+234
System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler,SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj)+2275
System.Data.SqlClient.SqlDataReader.ConsumeMetaData()+33
System.Data.SqlClient.SqlDataReader.get_MetaData()+86
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior,String resetOptionsString)+311
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(的CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,Boolean async)+987
System.Data.SqlClient.SqlCommand.RunExecuteReader(的CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,String 方法,DbAsyncResult结果)+162
System.Data.SqlClient.SqlCommand.RunExecuteReader(的CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,String 方法)+32
System.Data.SqlClient.SqlCommand.ExecuteReader(的CommandBehavior 行为,字符串方法)+141
System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(的CommandBehavior 行为)+12
System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader() +12 NHibernate.AdoNet.AbstractBatcher.ExecuteReader(IDbCommand cmd)+356 NHibernate.Driver.BatcherDataReaderWrapper..ctor(IBatcher batcher,IDbCommand命令)+183
NHibernate.Driver.BasicResultSetsCommand.GetReader(Nullable`1 commandTimeout)+432 NHibernate.Impl.MultiQueryImpl.DoList()+ 683
运行SQL事件探查器,执行以下命令
Error converting data type varbinary to geography
可以看出,根据传递给exec sp_executesql N'exec my-sproc-name @p0' @p0=0x0001000000FFFFFFFF01000000000000000C02000000574E6574546F706F6C6F677953756974652C2056657273696F6E3D312E372E332E31373239382C2043756C747572653D6E65757472616C2C205075626C69634B6579546F6B656E3D663538306130353031366562616461310C030000004947656F4150492C2056657273696F6E3D312E312E302E302C2043756C747572653D6E65757472616C2C205075626C69634B6579546F6B656E3D6131613064613764656634363536373805010000002E4769735368617270426C6F672E4E6574546F706F6C6F677953756974652E47656F6D6574726965732E506F696E74090000000B636F6F7264696E6174657308656E76656C6F70651047656F6D657472792B666163746F72791147656F6D657472792B75736572446174611147656F6D657472792B656E76656C6F70650D47656F6D657472792B737269641247656F6D657472792B64696D656E73696F6E1147656F6D657472792B626F756E646172791A47656F6D657472792B626F756E6461727944696D656E73696F6E040404020400040404404769735368617270426C6F672E4E6574546F706F6C6F677953756974652E47656F6D6574726965732E436F6F7264696E617465417272617953657175656E6365020000001B47656F4150492E47656F6D6574726965732E49456E76656C6F706503000000384769735368617270426C6F672E4E6574546F706F6C6F677953756974652E47656F6D6574726965732E47656F6D65747279466163746F7279020000001B47656F4150492E47656F6D6574726965732E49456E76656C6F706503000000081C47656F4150492E47656F6D6574726965732E44696D656E73696F6E73030000001B47656F4150492E47656F6D6574726965732E4947656F6D65747279030000001C47656F4150492E47656F6D6574726965732E44696D656E73696F6E73030000000200000009040000000A09050000000A0A0000000005FAFFFFFF1C47656F4150492E47656F6D6574726965732E44696D656E73696F6E73010000000776616C75655F5F000803000000000000000A01F9FFFFFFFAFFFFFF000000000504000000404769735368617270426C6F672E4E6574546F706F6C6F677953756974652E47656F6D6574726965732E436F6F7264696E617465417272617953657175656E6365010000000B636F6F7264696E61746573041F47656F4150492E47656F6D6574726965732E49436F6F7264696E6174655B5D030000000200000009080000000505000000384769735368617270426C6F672E4E6574546F706F6C6F677953756974652E47656F6D6574726965732E47656F6D65747279466163746F7279030000000E707265636973696F6E4D6F64656C19636F6F7264696E61746553657175656E6365466163746F72790473726964040400374769735368617270426C6F672E4E6574546F706F6C6F677953756974652E47656F6D6574726965732E507265636973696F6E4D6F64656C02000000474769735368617270426C6F672E4E6574546F706F6C6F677953756974652E47656F6D6574726965732E436F6F7264696E617465417272617953657175656E6365466163746F72790200000008020000000909000000090A000000000000000708000000000100000001000000041D47656F4150492E47656F6D6574726965732E49436F6F7264696E61746503000000090B0000000509000000374769735368617270426C6F672E4E6574546F706F6C6F677953756974652E47656F6D6574726965732E507265636973696F6E4D6F64656C02000000096D6F64656C54797065057363616C6504002147656F4150492E47656F6D6574726965732E507265636973696F6E4D6F64656C7303000000060200000005F4FFFFFF2147656F4150492E47656F6D6574726965732E507265636973696F6E4D6F64656C73010000000776616C75655F5F000803000000000000000000000000000000050A000000474769735368617270426C6F672E4E6574546F706F6C6F677953756974652E47656F6D6574726965732E436F6F7264696E617465417272617953657175656E6365466163746F72790000000002000000050B000000334769735368617270426C6F672E4E6574546F706F6C6F677953756974652E47656F6D6574726965732E436F6F7264696E6174650300000001780179017A000000060606020000000BEE77280AB404C0AD4D637B2DBA4940000000000000F8FF0B
并在命令中传递的值确定的值是不同的。
是否可以将类型传递给命名查询以执行我想要的操作?
答案 0 :(得分:3)
在检查NHibernate.Type.IType
作为第三个参数的SetParameter()
具体类型后,我想出来了。以下作品
private static SqlGeography GetPoint(double latitude, double longitude)
{
var geographyBuilder = new SqlGeographyBuilder();
geographyBuilder.SetSrid(4326);
geographyBuilder.BeginGeography(OpenGisGeographyType.Point);
geographyBuilder.BeginFigure(latitude, longitude);
geographyBuilder.EndFigure();
geographyBuilder.EndGeography();
return geographyBuilder.ConstructedGeography;
}
var query = Session.GetNamedQuery("my-sproc-name");
var point = GetPoint(latitude, longitude);
query.SetParameter(
"Location",
point,
new NHibernate.Spatial.Type.SqlGeographyType());
// my-sproc-name is mapped to return a collection of EntityA types
query.List<EntityA>();
可以在 NHibernate.Spatial.dll 程序集中找到NHibernate.Spatial.Type.SqlGeographyType
类型。