0
votes

I am getting an error with Dapper when I try selecting into a POCO called zones2.

public partial class ZONES2
{
    public int ID { get; set; }
    public SqlGeography LATLNG { get; set; }
}

This code produces an error.

  using (SqlConnection conn = new SqlConnection(connStr))
  {
     var zones = conn.Query<ZONES2>("select id,latlng from zones2");
  }

The error is:

System.Data.DataException was unhandled

HResult=-2146233087 Message=Error parsing column 1 (latlng=POLYGON ((50.804687175845011 -1.1351966857910156, 50.803927780711625 -1.1324286460876465, 50.804531229637192 -1.1276113986968994, 50.804870242468276 -1.1231803894042969, 50.80373792999972 -1.1219358444213867, 50.80011705805952 -1.1197042465209961, 50.796767162631618 -1.1166036128997803, 50.793932448170736 -1.1145973205566406, 50.791830036934492 -1.1151659488677979, 50.79201993602792 -1.1166250705718994, 50.789123891025056 -1.1191999912261963, 50.790316143034808 -1.1227444584776549, 50.790744884457638 -1.1259269714355469, 50.791687612108106 -1.127171516418457, 50.793932448170736 -1.1280298233032227, 50.794217283709372 -1.1288988590240479, 50.794786949579219 -1.1292421817779541, 50.795675343209147 -1.1292743682861328, 50.797408224520929 -1.129325783323452, 50.797587705782 -1.1320316791534424, 50.797784362743926 -1.1328577995300293, 50.797784362743926 -1.1339521408081055, 50.79802269251892 -1.1352215292627079, 50.799967877542073 -1.1342954635620117, 50.801562477015025 -1.1342542270608647, 50.804687175845011 -1.1351966857910156, 50.804687175845011 -1.1351966857910156)) - Object) Source=Dapper StackTrace: at Dapper.SqlMapper.ThrowDataException(Exception ex, Int32 index, IDataReader reader, Object value) in D:\Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:line 4153 at Deserialize0c167990-8477-4066-b090-cd522cbf0768(IDataReader ) at Dapper.SqlMapper.d__611.MoveNext() in D:\Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:line 1608 at System.Collections.Generic.List1..ctor(IEnumerable1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source) at Dapper.SqlMapper.Query[T](IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Boolean buffered, Nullable1 commandTimeout, Nullable1 commandType) in D:\Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:line 1479 at spacial.Program.Main(String[] args) in c:\users\paul\Google Drive\Visual Studio 2015\spacial\spacial\Program.cs:line 17 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException: HResult=-2147467262 Message=[A]Microsoft.SqlServer.Types.SqlGeography cannot be cast to [B]Microsoft.SqlServer.Types.SqlGeography. Type A originates from 'Microsoft.SqlServer.Types, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' in the context 'Default' at location 'C:\WINDOWS\assembly\GAC_MSIL\Microsoft.SqlServer.Types\10.0.0.0__89845dcd8080cc91\Microsoft.SqlServer.Types.dll'. Type B originates from 'Microsoft.SqlServer.Types, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' in the context 'Default' at location 'C:\WINDOWS\assembly\GAC_MSIL\Microsoft.SqlServer.Types\11.0.0.0__89845dcd8080cc91\Microsoft.SqlServer.Types.dll'. Source=Anonymously Hosted DynamicMethods Assembly StackTrace: at Deserialize0c167990-8477-4066-b090-cd522cbf0768(IDataReader ) InnerException:

However when i try this code it runs ok.(N.B. dynamic type)

     using (SqlConnection conn = new SqlConnection(connStr))
     {
         var zones = conn.Query<dynamic>("select id,latlng from zones2");
     }

I would like to know why I get an error in the first code?

1
I expect it is the version information mentioned in the inner exception.Paul Stanley
You probably just need a binding redirect from 10 to 11 for the type. Are you familiar with binding redirects? (app.config / web.config)Marc Gravell♦
No I am not aware of Binding redirectsPaul Stanley
Marc you are a star.Paul Stanley

1 Answers

-1
votes

I highlighted the reference to Microsoft.SqlServer.Types opened up the properties window and changed "Use specific version" from true to false. I do not get an error now.