Dapper C#:无参数默认构造函数或一个匹配签名错误
我想获得给定表的分区列表。
public record Partition(string PartitionName, int OrdinalPosition);
public class MysqlService
{
//...
public IEnumerable<Partition> GetPartitions(string tableName)
{
using var conn = new MySqlConnection(_dbConnString);
conn.Open();
return conn.Query<Partition>(
$"SELECT PARTITION_NAME, PARTITION_ORDINAL_POSITION " +
$"FROM information_schema.partitions WHERE TABLE_NAME = '{tableName}';");
}
}
但是我的代码给出了一个错误,例如
Unhandled exception. System.InvalidOperationException: A parameterless default constructor or one matching signature (System.String PARTITION_NAME, System.UInt64 PARTITION_ORDINAL_POSITION) is required for Services.Partition materialization
at Dapper.SqlMapper.GenerateDeserializerFromMap(Type type, IDataReader reader, Int32 startBound, Int32 length, Boolean returnNullIfFirstMissing, ILGenerator il) in /_/Dapper/SqlMapper.cs:line 3297
at Dapper.SqlMapper.GetTypeDeserializerImpl(Type type, IDataReader reader, Int32 startBound, Int32 length, Boolean returnNullIfFirstMissing) in /_/Dapper/SqlMapper.cs:line 3131
at Dapper.SqlMapper.TypeDeserializerCache.GetReader(IDataReader reader, Int32 startBound, Int32 length, Boolean returnNullIfFirstMissing) in /_/Dapper/SqlMapper.TypeDeserializerCache.cs:line 151
at Dapper.SqlMapper.TypeDeserializerCache.GetReader(Type type, IDataReader reader, Int32 startBound, Int32 length, Boolean returnNullIfFirstMissing) in /_/Dapper/SqlMapper.TypeDeserializerCache.cs:line 50
at Dapper.SqlMapper.GetTypeDeserializer(Type type, IDataReader reader, Int32 startBound, Int32 length, Boolean returnNullIfFirstMissing) in /_/Dapper/SqlMapper.cs:line 3085
at Dapper.SqlMapper.GetDeserializer(Type type, IDataReader reader, Int32 startBound, Int32 length, Boolean returnNullIfFirstMissing) in /_/Dapper/SqlMapper.cs:line 1835
at Dapper.SqlMapper.QueryImpl[T](IDbConnection cnn, CommandDefinition command, Type effectiveType)+MoveNext() in /_/Dapper/SqlMapper.cs:line 1105
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at Dapper.SqlMapper.Query[T](IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Boolean buffered, Nullable`1 commandTimeout, Nullable`1 commandType) in /_/Dapper/SqlMapper.cs:line 734
...
我该如何解决?
我尝试使用uint64
而不是int
用于ordinalPosition
的类型,但是发生了相同的错误。
我的环境:
- c#.net核心控制台应用程序(net6.0)
- dapper 2.0.123
- mysql 8.0
I would like to get list of partitions for a given table.
public record Partition(string PartitionName, int OrdinalPosition);
public class MysqlService
{
//...
public IEnumerable<Partition> GetPartitions(string tableName)
{
using var conn = new MySqlConnection(_dbConnString);
conn.Open();
return conn.Query<Partition>(
quot;SELECT PARTITION_NAME, PARTITION_ORDINAL_POSITION " +
quot;FROM information_schema.partitions WHERE TABLE_NAME = '{tableName}';");
}
}
But my code gives an error like
Unhandled exception. System.InvalidOperationException: A parameterless default constructor or one matching signature (System.String PARTITION_NAME, System.UInt64 PARTITION_ORDINAL_POSITION) is required for Services.Partition materialization
at Dapper.SqlMapper.GenerateDeserializerFromMap(Type type, IDataReader reader, Int32 startBound, Int32 length, Boolean returnNullIfFirstMissing, ILGenerator il) in /_/Dapper/SqlMapper.cs:line 3297
at Dapper.SqlMapper.GetTypeDeserializerImpl(Type type, IDataReader reader, Int32 startBound, Int32 length, Boolean returnNullIfFirstMissing) in /_/Dapper/SqlMapper.cs:line 3131
at Dapper.SqlMapper.TypeDeserializerCache.GetReader(IDataReader reader, Int32 startBound, Int32 length, Boolean returnNullIfFirstMissing) in /_/Dapper/SqlMapper.TypeDeserializerCache.cs:line 151
at Dapper.SqlMapper.TypeDeserializerCache.GetReader(Type type, IDataReader reader, Int32 startBound, Int32 length, Boolean returnNullIfFirstMissing) in /_/Dapper/SqlMapper.TypeDeserializerCache.cs:line 50
at Dapper.SqlMapper.GetTypeDeserializer(Type type, IDataReader reader, Int32 startBound, Int32 length, Boolean returnNullIfFirstMissing) in /_/Dapper/SqlMapper.cs:line 3085
at Dapper.SqlMapper.GetDeserializer(Type type, IDataReader reader, Int32 startBound, Int32 length, Boolean returnNullIfFirstMissing) in /_/Dapper/SqlMapper.cs:line 1835
at Dapper.SqlMapper.QueryImpl[T](IDbConnection cnn, CommandDefinition command, Type effectiveType)+MoveNext() in /_/Dapper/SqlMapper.cs:line 1105
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at Dapper.SqlMapper.Query[T](IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Boolean buffered, Nullable`1 commandTimeout, Nullable`1 commandType) in /_/Dapper/SqlMapper.cs:line 734
...
How can I fix this?
I tried to use UInt64
instead of int
for the type of OrdinalPosition
, but the same error occured.
My Environment:
- C# .NET Core Console Application (net6.0)
- Dapper 2.0.123
- MySQL 8.0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
类型和名称必须匹配(或使用自定义映射)。
您可以使用别名在C#中具有“较好”的名称:
公共记录partion(字符串partitionName,uint64 ordinalPosition)
您还应该处理您通过的潜在SQL注入问题
用wera-sare构建字符串。
Dapper使它变得足够容易:
Types and names must match (or use a custom mapping).
You can use aliases to have "nicer" names in C#:
public record Partion(string PartitionName, uint64 OrdinalPosition)
You should also deal with the potential SQL injection issue you have by
building a string with the WHERE-clause.
Dapper makes this easy enough:
同样的例外。您可以这样创建CTOR:
Same Exception. You can create ctor like this: