TargetInvocationException was unhandled by user code

时间:2018-02-16 07:35:45

标签: c#

I'm stuck! I have this 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);

Where h.GreenPoly has value {SRID=4326;POLYGON ((-112.51643746 33.49170131, -112.51694319 33.4917729, -112.51685776 33.49219669, -112.51635201 33.49212509, -112.51643746 33.49170131))} and it's type is System.Data.Entity.Spatial.DbGeography

But the line where I calculate linestringBack throws an exception

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.

I've tried replacing it with

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);

but I'm still getting the same error and I can't figure out the cause. Any ideas?

EDIT:

Image1
Image2
Image3
Image4

1 个答案:

答案 0 :(得分:0)

我找到了解决问题的方法。它似乎是关于Microsoft.SqlServer.Types的问题。我使用的是版本11,它在将其更新为14(在VS2015和VS2017中测试)后工作

对于那些可能会遇到同样问题的人:

  1. 右键点击您的项目 - &gt;管理NuGet包......

  2. 在搜索标签(已安装区域)中输入Microsoft.SqlServer.Types

  3. 点击它并更新到版本14

  4. 在任何地方搜索都使用Microsoft.SqlServer.Types Web.config文件,并将newVersion更改为14.0.0.0