不支持 Dapper PostgreSQL 复合类型

问题描述 投票:0回答:1

考虑模型

public record Model
(
  UserTimestamp A, 
  UserTimestamp B, 
  UserTimestamp[] C
)

其中

UserTimestamp
是 postgresql 中的复合类型
usertimestamp (int user_id, timestamp timestamp)

和 .NET 等效项

public record UserTimestamp(int UserId, DateTime Timestamp)

并且它通过映射到 npgsql

DataSourceBuilder.MapComposite<UserTimestamp>("usertimestamp");
在存储库中

Dapper 抛出带有消息的

NotSupportedException
member "A" of type "Model" cannot be used as a parameter

但是,如果我将类型更改为

public record Model
(
  UserTimestamp[] A, 
  UserTimestamp[] B, 
  UserTimestamp[] C
)

并相应地更改我的数据库表 并插入一个数组,其中包含我尝试插入的一个元素,然后它似乎可以完美地工作。

不支持单一复合类型背后是否有任何原因,或者我在这里遗漏了什么?

c# postgresql dapper npgsql
1个回答
0
投票

解决了。 经过一番挖掘并尝试让它与 NodaTime 一起使用(因为这是 Npgsql 的推荐方式) 我偶然发现了这个线程,在那里我发现了

SqlMapper.AddTypeMap
方法

所以

SqlMapper.AddTypeMap(typeof(UserTimestamp), DbType.Object);

启用 dapper 让 Npgsql 处理类型

© www.soinside.com 2019 - 2024. All rights reserved.