可空 Guid 的 Dapper 问题?类型
Dapper problem with a nullable Guid? types
我对 Dapper 和 Guid?
类型有疑问。我创建了那个演示项目:
数据库MySQL (table entities
):
CREATE TABLE `entities` (
`id` int NOT NULL AUTO_INCREMENT,
`notnullguid` char(36) NOT NULL,
`nullguid` char(36) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
第一个 Guid
字段 - 不为空,第二个 - 可为空(默认值为 NULL
)。
演示数据:
id
notnullguid
nullguid
1
7b137c4e-38db-11eb-adec-a85e45e499a0
NULL
2
f57b9036-38db-11eb-adec-a85e45e499a0
f57b906b-38db-11eb-adec-a85e45e499a0
我的实体:
public class Entity
{
public int Id {get; set; }
public Guid NotNullGuid { get; set; }
public Guid? NullGuid { get; set; }
}
我的查询:
//PROBLEM:
Entity e1 = db.Query<Entity>("SELECT * FROM entities WHERE id=1").First(); //Not working
//WORKING - all variations:
Entity e2 = db.Query<Entity>("SELECT * FROM entities WHERE id=2").First(); //Working
Entity e3 = db.Query<Entity>("SELECT notnullguid FROM ents WHERE id=1;").First(); //Working
Entity e4 = db.Query<Entity>("SELECT notnullguid, NULL as nullguid FROM ents WHERE id=1;").First(); //Working
Entity e5 = db.Query<Entity>("SELECT notnullguid, IFNULL(nullguid, NULL) FROM ents WHERE id=1;").First(); //Working
如果我得到 id=1
我有那个例外:
System.Data.DataException: 'Error parsing column 2
(nullguid=7b137c4e-38db-11eb-adec-a85e45e499a0 - Object)' This
exception was originally thrown at this call stack:
System.Data.DataException HResult=0x80131501 Message=Error parsing
column 2 (nullguid=7b137c4e-38db-11eb-adec-a85e45e499a0 - Object)
Source=Dapper StackTrace: at
Dapper.SqlMapper.ThrowDataException(Exception ex, Int32 index,
IDataReader reader, Object value) in //Dapper/SqlMapper.cs:line 3665
at Dapper.SqlMapper.d__1401.MoveNext() in /_/Dapper/SqlMapper.cs:line 1102 at System.Collections.Generic.List
1..ctor(IEnumerable1 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, Nullable1 commandTimeout, Nullable
1 commandType) in //Dapper/SqlMapper.cs:line
725 at DapperGuidParsing.Program.Main(String[] args) in
C:\Users\Dmytro\source\repos\DapperGuidParsing\DapperGuidParsing\Program.cs:line
21
This exception was originally thrown at this call stack:
System.ThrowHelper.ThrowArgumentOutOfRangeException(System.ExceptionArgument,
System.ExceptionResource)
System.Text.UTF8Encoding.GetString(byte[], int, int)
MySql.Data.MySqlClient.NativeDriver.ReadColumnValue(int, MySql.Data.MySqlClient.MySqlField, MySql.Data.Types.IMySqlValue)
MySql.Data.MySqlClient.ResultSet.this[int].get(int)
MySql.Data.MySqlClient.MySqlDataReader.GetFieldValue(int, bool)
MySql.Data.MySqlClient.MySqlDataReader.GetValue(int)
MySql.Data.MySqlClient.MySqlDataReader.this[int].get(int) Inner Exception 1: ArgumentOutOfRangeException: Non-negative number
required.
为什么? :|我该如何解决这个问题?
一些技术信息:.NET Core 3.1 Console App
、Dapper 2.0.78
、MySql.Data 8.0.22
。
安装 Nuget 包时一切正常 MySqlConnector
//Working
IDbConnection db = new MySqlConnector.MySqlConnection(this.ConnectionString);
//Not working (default MySqlConnection from MySql.Data package)
//IDbConnection db = new MySql.Data.MySqlClient.MySqlConnection(this.ConnectionString);
我对 Dapper 和 Guid?
类型有疑问。我创建了那个演示项目:
数据库MySQL (table entities
):
CREATE TABLE `entities` (
`id` int NOT NULL AUTO_INCREMENT,
`notnullguid` char(36) NOT NULL,
`nullguid` char(36) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
第一个 Guid
字段 - 不为空,第二个 - 可为空(默认值为 NULL
)。
演示数据:
id | notnullguid | nullguid |
---|---|---|
1 | 7b137c4e-38db-11eb-adec-a85e45e499a0 | NULL |
2 | f57b9036-38db-11eb-adec-a85e45e499a0 | f57b906b-38db-11eb-adec-a85e45e499a0 |
我的实体:
public class Entity
{
public int Id {get; set; }
public Guid NotNullGuid { get; set; }
public Guid? NullGuid { get; set; }
}
我的查询:
//PROBLEM:
Entity e1 = db.Query<Entity>("SELECT * FROM entities WHERE id=1").First(); //Not working
//WORKING - all variations:
Entity e2 = db.Query<Entity>("SELECT * FROM entities WHERE id=2").First(); //Working
Entity e3 = db.Query<Entity>("SELECT notnullguid FROM ents WHERE id=1;").First(); //Working
Entity e4 = db.Query<Entity>("SELECT notnullguid, NULL as nullguid FROM ents WHERE id=1;").First(); //Working
Entity e5 = db.Query<Entity>("SELECT notnullguid, IFNULL(nullguid, NULL) FROM ents WHERE id=1;").First(); //Working
如果我得到 id=1
我有那个例外:
System.Data.DataException: 'Error parsing column 2 (nullguid=7b137c4e-38db-11eb-adec-a85e45e499a0 - Object)' This exception was originally thrown at this call stack:
System.Data.DataException HResult=0x80131501 Message=Error parsing column 2 (nullguid=7b137c4e-38db-11eb-adec-a85e45e499a0 - Object)
Source=Dapper StackTrace: at Dapper.SqlMapper.ThrowDataException(Exception ex, Int32 index, IDataReader reader, Object value) in //Dapper/SqlMapper.cs:line 3665 at Dapper.SqlMapper.d__1401.MoveNext() in /_/Dapper/SqlMapper.cs:line 1102 at System.Collections.Generic.List
1..ctor(IEnumerable1 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, Nullable1 commandTimeout, Nullable
1 commandType) in //Dapper/SqlMapper.cs:line 725 at DapperGuidParsing.Program.Main(String[] args) in C:\Users\Dmytro\source\repos\DapperGuidParsing\DapperGuidParsing\Program.cs:line 21This exception was originally thrown at this call stack: System.ThrowHelper.ThrowArgumentOutOfRangeException(System.ExceptionArgument, System.ExceptionResource) System.Text.UTF8Encoding.GetString(byte[], int, int) MySql.Data.MySqlClient.NativeDriver.ReadColumnValue(int, MySql.Data.MySqlClient.MySqlField, MySql.Data.Types.IMySqlValue) MySql.Data.MySqlClient.ResultSet.this[int].get(int) MySql.Data.MySqlClient.MySqlDataReader.GetFieldValue(int, bool) MySql.Data.MySqlClient.MySqlDataReader.GetValue(int) MySql.Data.MySqlClient.MySqlDataReader.this[int].get(int) Inner Exception 1: ArgumentOutOfRangeException: Non-negative number required.
为什么? :|我该如何解决这个问题?
一些技术信息:.NET Core 3.1 Console App
、Dapper 2.0.78
、MySql.Data 8.0.22
。
安装 Nuget 包时一切正常 MySqlConnector
//Working
IDbConnection db = new MySqlConnector.MySqlConnection(this.ConnectionString);
//Not working (default MySqlConnection from MySql.Data package)
//IDbConnection db = new MySql.Data.MySqlClient.MySqlConnection(this.ConnectionString);