Gaurav Singh
Gaurav Singh

Reputation: 31

MySql with Entity Framework 6: Binary(18) to byte[] conversion fails for Subtable

I have a table Users with various column and another table UserJob.

Users table structure:

public byte[] Id { get; set; }
public string language{ get; set; }
public string Password{ get; set; }

UserJob table structure

public byte[] UId { get; set; }
public string MyJob{ get; set; }

Now UId in UserJob table is foreign key for User table.

All the mapping are correctly done via Code first from database technique but when i fetch DBSet from Db i only get parent table and get a exception for UserJob table as Index was outside the bounds of the array.

Now Uid in Db is has Datatype Binary(18).

SomeHow conversion from Binary(18) to byte[] fails for subtable but works well for parent table. (In this case ID of User table is also saved as Binary(18) and gets populated pretty well as byte[18] in the output).

So i guess this problem is for Subtable only.

Exception is in MySql.Data dll.

 at MySql.Data.Types.MySqlBinary.EscapeByteArray(Byte[] bytes, Int32 length, MySqlPacket packet)
   at MySql.Data.Types.MySqlBinary.MySql.Data.Types.IMySqlValue.WriteValue(MySqlPacket packet, Boolean binary, Object val, Int32 length)
   at MySql.Data.MySqlClient.MySqlParameter.Serialize(MySqlPacket packet, Boolean binary, MySqlConnectionStringBuilder settings)
   at MySql.Data.MySqlClient.Statement.SerializeParameter(MySqlParameterCollection parameters, MySqlPacket packet, String parmName, Int32 parameterIndex)
   at MySql.Data.MySqlClient.Statement.InternalBindParameters(String sql, MySqlParameterCollection parameters, MySqlPacket packet)
   at MySql.Data.MySqlClient.Statement.BindParameters()
   at MySql.Data.MySqlClient.PreparableStatement.Execute()
   at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
   at MySql.Data.Entity.EFMySqlCommand.ExecuteDbDataReader(CommandBehavior behavior)

Asked in mysql forums also, but no answer. https://forums.mysql.com/read.php?38,651946,651946 MySql Connector: 6.10.7

If not solution, any suggestion to go near the problem is also welcomed.

Upvotes: 0

Views: 419

Answers (2)

Gaurav Singh
Gaurav Singh

Reputation: 31

Workaround for this issue is to set maxlength for your parameter. SomeHow MySql.Data.dll always take parameter length as 128(This is max length) instead of actual parameter length in above case.

So if you set [MaxLength(18)] for your parameter (Or whatever is your parameter maxlength is in the database), it won't break when it try to map your parameter in MySQl.Data.dll.

Hope this helps someone.

Upvotes: 0

Bradley Grainger
Bradley Grainger

Reputation: 28162

This is probably a bug in MySql.Data. For example, this code crashes with an IndexOutOfRangeException:

using (var command = new MySqlCommand("select @param", connection))
{
    command.Parameters.AddWithValue("@param", new byte[16]).Size = 18;
    command.ExecuteScalar();
}

It seems likely that somewhere in the MySql.Data library, the MySqlParameter.Size property is being set to a value larger than the length of the parameter value.

If you can't figure out a workaround, the next best thing to do would be file a bug report (with a concise sample to reproduce the problem) at https://bugs.mysql.com/.

Upvotes: 2

Related Questions