I'm using C#, .NET framework 4.5 (with intentions to upgrade to .NET 5), and PetaPoco as my ORM.
I have a table named Jurisdiction with the following field definition:
CREATE Table [Jurisdiction]
...
[GeographicArea] [geography] NULL
...
);
In my database layer, I have the following:
var sql = @"
SELECT
Jurisdiction.*,
State.StateName
FROM
Jurisdiction
LEFT OUTER JOIN State ON Jurisdiction.StateId = State.StateId
";
if (where.Count > 0)
{
sql += $" WHERE {string.Join(" AND ", where)}";
}
sql += orderBy;
var jurisdictions = _database.Query<T>(sql, parameters.ToArray()).ToList();
However, when this method runs I get the following exception:
'Could not load file or assembly 'Microsoft.SqlServer.Types, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' or one of its dependencies. The system cannot find the file specified.'
This was causing an issue when I let the database.tt file automatically generate the POCO definition for Jurisdiction so what I did to the tt file was add the following so that it would stop trying to automatically use the SqlServers.Geography type:
tables["Jurisdiction"]["GeographicArea"].PropertyType="string";
However, even with defining the field as a string, it is still throwing the exception, regardless of if I have the Microsoft.SqlServer.Types library added to the project or not.
How can I can not fool with the Microsoft.SqlServer.Types library using PetaPoco?
I was ultimately able to resolve my issue, though it was very much involved. In my query, instead of doing a
SELECT *I had to spell out each column and manually convert the geography value using:Then in my view model, I set the insert and update templates using:
Finally in my service layer, I created the following two methods to get/update the geography column using: