簡體   English   中英

用戶代碼未處理TargetInvocationException

[英]TargetInvocationException was unhandled by user code

我被卡住了! 我有這個代碼

System.Data.Entity.Spatial.DbGeography pointLefttop = h.GreenPoly == null || h.GreenPoly.PointCount < 4 ? null : h.GreenPoly.PointAt(1);
System.Data.Entity.Spatial.DbGeography pointRighttop = h.GreenPoly == null || h.GreenPoly.PointCount < 4 ? null : h.GreenPoly.PointAt(2);
DbGeography linestringBack = pointLefttop == null || pointRighttop == null ? DbGeography.LineFromText("LINESTRING(0 0, 0 0)", 4326) : System.Data.Entity.Spatial.DbGeography.LineFromText("LINESTRING(" + pointLefttop.Longitude + " " + pointLefttop.Latitude + ", " + pointRighttop.Longitude + " " + pointRighttop.Latitude + ")", 4326);

其中h.GreenPoly值是{SRID=4326;POLYGON ((-112.51643746 33.49170131, -112.51694319 33.4917729, -112.51685776 33.49219669, -112.51635201 33.49212509, -112.51643746 33.49170131))} ,其類型是System.Data.Entity.Spatial.DbGeography

但是我計算linestringBack的行拋出異常

An exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll but was not handled in user code

Additional information: Exception has been thrown by the target of an invocation.

我嘗試用替換

DbGeography linestringBack = pointLefttop == null || pointRighttop == null ? DbGeography.LineFromText("LINESTRING(0.0, 0.0)", DbGeography.DefaultCoordinateSystemId) : System.Data.Entity.Spatial.DbGeography.LineFromText(
                                    string.Format("LINESTRING({0} {1}, {2} {3})",
                                    pointLefttop.Longitude.Value.ToString(CultureInfo.InvariantCulture), pointLefttop.Latitude.Value.ToString(CultureInfo.InvariantCulture), pointRighttop.Longitude.Value.ToString(CultureInfo.InvariantCulture), pointRighttop.Latitude.Value.ToString(CultureInfo.InvariantCulture))
                                    , DbGeography.DefaultCoordinateSystemId);

但我仍然遇到相同的錯誤,無法找出原因。 有任何想法嗎?

編輯:

圖片1
圖片2
圖3
圖4

我找到了解決我問題的方法。 看來這與Microsoft.SqlServer.Types有關。 我使用的是版本11,並將其更新為14后可以正常工作(在VS2015和VS2017中測試)

對於那些可能會有相同問題的人:

  1. 右鍵單擊您的項目->管理NuGet軟件包...

  2. 在搜索選項卡(已安裝區域)中,鍵入Microsoft.SqlServer.Types

  3. 單擊它並更新到版本14

  4. 搜索到處都使用Microsoft.SqlServer.Types類的Web.config文件,並將newVersion更改為14.0.0.0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM