Bug #96499 MySqlException when inserting a MySqlGeometry value
Submitted: 10 Aug 2019 13:36 Modified: 12 Mar 2020 18:37
Reporter: Bradley Grainger (OCA) Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / NET Severity:S2 (Serious)
Version:8.0.17 OS:Windows (10)
Assigned to: CPU Architecture:Other (x64)

[10 Aug 2019 13:36] Bradley Grainger
Description:
Attempting to insert a MySqlGeometry value in a GEOMETRY column throws a MySqlException: "Cannot get geometry object from data you send to the GEOMETRY field".

Setting "MySqlParameter.Value = geometry" fails; setting "MySqlParameter.Value = geometry.Value" succeeds. The expected behaviour is that both ways of specifying the parameter value would function identically.

How to repeat:
Execute the following SQL:

DROP TABLE IF EXISTS geometries;
CREATE TABLE geometries(id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, data GEOMETRY);

Run the following C# code:

var geometry = new MySqlGeometry(1, 1);

using (var connection = new MySqlConnection("..."))
{
	connection.Open();
	using (var command = connection.CreateCommand())
	{
		command.CommandText = "INSERT INTO geometries(data) VALUES(@data);";

		// workaround: use 'geometry.Value' instead of 'geometry'
		command.Parameters.AddWithValue("@data", geometry);

		// throws MySqlException: Cannot get geometry object from data you send to the GEOMETRY field
		command.ExecuteNonQuery();
	}
}
[11 Aug 2019 12:04] MySQL Verification Team
Hello Bradley,

Thank you for the report and test case.

regards,
Umesh
[12 Mar 2020 17:10] Gustavo Cuatepotzo
Posted by developer:
 
the problem was when method AddWithValue internally invoke SetTypeFromValue to infer the Datatype from the value, it was getting in response a Blob type instead of MySqlGeometry type, this is corrected and also resolve Bug #30169715.
[12 Mar 2020 18:37] Christine Cole
Posted by developer:
 
Fixed as of the upcoming MySQL Connector/NET 8.0.21 release, and here's the proposed changelog entry from the documentation team:

The BLOB type was inferred internally when a value or object of type
MySqlGeometry was used in different situations, which caused to server to
return either zero matching rows or an exception.

Thank you for the bug report.