11

我有实体“点”,其中包含 ID、文本和地理坐标。

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

这是表的ts sql代码和插入的存储过程。我有使用 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);
    }
}

c#-类是:

public class Point
{
    public int Id { get; set; }
    public SqlGeography Coords { get; set; }
    public string Text { get; set; }
}

并且存储库有方法

public Point InsertPoint(string text, SqlGeography coords)
    {
        using (var requester = GetRequester())
        {
            return requester.ExecuteProc<Point>("InsertPoint", new { text, coords }).FirstOrDefault();
        }
    }

当我将此类系统用于任何其他类时,一切正常,但映射存在问题,我认为这是因为 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);

我有一个例外The member coords of type Microsoft.SqlServer.Types.SqlGeography cannot be used as a parameter value

映射这种类型有什么秘密吗?

4

3 回答 3

14

Dapper 1.32 now includes direct support for this. Your code should now simply work.

于 2014-08-28T08:31:11.347 回答
1

Dapper 不支持 DB Provider 特定的数据类型。在你的情况下,它的地理。

Dapper 没有特定于 DB 的实现细节,它适用于所有 .net ado 提供程序,包括 sqlite、sqlce、firebird、oracle、MySQL 和 SQL Server

为了用 Dapper 处理这个参数,你必须编写自己的处理方法。例如,请参阅此答案

祝你好运

于 2012-08-23T16:55:46.270 回答
1

我遇到了类似的问题。我发现 Dapper 可以Microsoft.SqlServer.Types.SqlGeography很好地映射结果字段,但是将它们用作参数不起作用。

我已经修改了 SqlMapper.cs 文件以包含对这种类型的支持。你可以在这里看到要点:https ://gist.github.com/bmckenzie/4961483

单击“修订”以查看我更改的内容。

于 2013-02-15T16:27:13.710 回答