gpt4 book ai didi

c# - 使用 Dapper 映射 SqlGeography

转载 作者:太空狗 更新时间:2023-10-29 17:43:34 25 4
gpt4 key购买 nike

我有实体“点”,其中包含 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 的类(class):

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);

而且我有一个异常Microsoft.SqlServer.Types.SqlGeography 类型的成员坐标不能用作参数值

映射那种类型有什么 secret 吗?

最佳答案

Dapper 1.32 现在 includes direct support for this .您的代码现在应该可以正常工作了

关于c# - 使用 Dapper 映射 SqlGeography,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12090549/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com