J'ai l'entité "Point", qui contient les coordonnées Id, Text et geography.
CREATE TABLE [Point] (
[Id] INT IDENTITY CONSTRAINT [PK_Point_Id] PRIMARY KEY,
[Coords] GEOGRAPHY NOT NULL,
[Text] NVARCHAR(32) NOT NULL,
[CreationDate] DATETIME NOT NULL,
[IsDeleted] BIT NOT NULL DEFAULT(0)
)
CREATE PROCEDURE [InsertPoint]
@text NVARCHAR(MAX),
@coords GEOGRAPHY
AS BEGIN
INSERT INTO [Point](Text, Coords, CreationDate)
VALUES(@text, @coords, GETUTCDATE())
SELECT * FROM [Point] WHERE [Id] = SCOPE_IDENTITY()
END
C'est le code SQL de la table et la procédure stockée d'insertion. J'ai classe pour utiliser dapper:
public class DapperRequester : IDisposable {
private readonly SqlConnection _connection;
private SqlTransaction _transaction;
public DapperRequester(string connectionString) {
_connection = new SqlConnection(connectionString);
_connection.Open();
}
public void Dispose() {
_connection.Close();
}
public void BeginTransaction() {
_transaction = _connection.BeginTransaction();
}
public void CommitTransaction() {
_transaction.Commit();
}
public void RollbackTransaction() {
_transaction.Rollback();
}
public void Query(string query, object parameters = null) {
Dapper.SqlMapper.Execute(_connection, query, parameters, transaction: _transaction);
}
public void QueryProc(string procName, object parameters = null) {
Dapper.SqlMapper.Execute(_connection, procName, parameters, commandType: CommandType.StoredProcedure, transaction: _transaction);
}
public IEnumerable<T> Execute<T>(string query, object parameters = null) {
return Dapper.SqlMapper.Query<T>(_connection, query, parameters, transaction: _transaction);
}
public IEnumerable<dynamic> ExecuteProc(string procName, object parameters = null) {
return Dapper.SqlMapper.Query(_connection, procName, parameters,
commandType: CommandType.StoredProcedure, transaction: _transaction);
}
public IEnumerable<T> ExecuteProc<T>(string procName, object parameters = null) {
return Dapper.SqlMapper.Query<T>(_connection, procName, parameters,
commandType: CommandType.StoredProcedure, transaction: _transaction);
}
}
La classe c # est:
public class Point
{
public int Id { get; set; }
public SqlGeography Coords { get; set; }
public string Text { get; set; }
}
Et dépôt a méthode
public Point InsertPoint(string text, SqlGeography coords)
{
using (var requester = GetRequester())
{
return requester.ExecuteProc<Point>("InsertPoint", new { text, coords }).FirstOrDefault();
}
}
Quand j'utilise un tel système pour n'importe quelle autre classe, tout va bien, mais il y a un problème de mappage, je pense que c'est à cause du type SqlGeography.
SqlGeography coords = new SqlGeography();
coords = SqlGeography.Point(10.5, 15.5, 4326);
Point point = new Point { Coords = coords, Text = "Text" };
point = Repositories.PointRepository.InsertPoint(point.Text, point.Coords);
Et j'ai une exception The member coords of type Microsoft.SqlServer.Types.SqlGeography cannot be used as a parameter value
Y a-t-il un secret de cartographie de ce type?
Dapper 1.32 inclut désormais un support direct pour cela . Votre code devrait maintenant fonctionner simplement .
J'ai rencontré un problème similaire. J'ai trouvé que Dapper mapperait les champs résultants sur Microsoft.SqlServer.Types.SqlGeography
, mais les utiliser comme paramètres ne fonctionnait pas.
J'ai modifié le fichier SqlMapper.cs pour inclure le support pour ce type. Vous pouvez voir la liste ici: https://gist.github.com/bmckenzie/4961483
Cliquez sur "Révisions" pour voir ce que j'ai changé.