简体   繁体   English

使用Dapper映射SqlGeography

[英]Mapping SqlGeography with Dapper

I have entity "Point", that contains Id, Text and geography coordinates. 我有实体“点”,包含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

This is ts sql code of table and stored procedure of inserting. 这是表的sql代码和插入的存储过程。 I have class for using 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#-class is : c#-class是:

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

And repository has method 而仓库有方法

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

When I use such system for any other class, everything is okey, but there is a problem with mapping, I think it is because of SqlGeography type.. Using : 当我将这样的系统用于任何其他类时,一切都很好,但是映射存在问题,我认为这是因为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);

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

Is there some secret of mapping that type? 是否存在映射该类型的秘密?

Dapper 1.32 now includes direct support for this . Dapper 1.32现在包括对此的直接支持 Your code should now simply work . 您的代码现在应该可以正常运行

Dapper does not support DB Provider specific data types. Dapper不支持DB Provider特定的数据类型。 In your case its GEOGRAPHY. 在你的情况下它的地理。

Dapper has no DB specific implementation details, it works across all .net ado providers including sqlite, sqlce, firebird, oracle, MySQL and SQL Server Dapper没有特定于DB的实现细节,它适用于所有.net ado提供程序,包括sqlite,sqlce,firebird,oracle,MySQL和SQL Server

In order to handle this param with Dapper, you would have to write your own handling for it. 为了使用Dapper处理这个参数,你必须为它编写自己的处理。 For an example, see this answer . 例如,请参阅此答案

Good luck 祝好运

I ran into a similar problem. 我遇到了类似的问题。 I found that Dapper would map resultant fields to Microsoft.SqlServer.Types.SqlGeography just fine, but using them as parameters didn't work. 我发现Dapper会将结果字段映射到Microsoft.SqlServer.Types.SqlGeography就好了,但是使用它们作为参数不起作用。

I've modified the SqlMapper.cs file to include support for this type. 我修改了SqlMapper.cs文件以包含对此类型的支持。 You can see the Gist here: https://gist.github.com/bmckenzie/4961483 你可以在这里看到Gist: https//gist.github.com/bmckenzie/4961483

Click on "Revisions" to see what I changed. 单击“修订”以查看我更改的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM