0

I am trying to migrate a database from MySQL to SQLServer using Dapper.

Almost everything is ok except with TinyInt fields.

Whatever the type I specify in my C# code, Dapper systematically throws an exception:

{"Error parsing column 18 (COLUMN_WITH_PROBLEM=0 - SByte)"}.

It seems to be related with the fact that the value of one of the record in the MySQL database is null

I've tried:

  • int, int?
  • byte, byte?
  • Byte, Byte?
  • sbyte, sbyte?
  • SByte, SByte?
  • short, short?
  • and even boolean?

Yet I always have the same issue.

What type do I have to specify in my C# code to avoid this error?

Obviously I can't change the column type in the Database.

E.Brudy
  • 23
  • 7
  • Which ones have you tried? – juharr Jan 17 '17 at 15:12
  • @juharr it is in my question : int, int?, byte, byte?, Byte Byte?, sbyte, sbyte?, SByte, SByte? and even boolean? – E.Brudy Jan 17 '17 at 15:19
  • Is the Tinyint column signed or unsigned? – Bridge Jan 17 '17 at 15:27
  • A Tinyint in sql server should map to Byte in .Net... I understand you already tried it with no luck. can you post some relevant code as well? – ATC Jan 17 '17 at 15:28
  • @Bridge in mySQL tinyint is signed but in my case the only values I have are 0 and null – E.Brudy Jan 17 '17 at 15:29
  • I am using Dapper so my code is simply await idbOrigine.QueryAsync(requeteSelect) and Dapper is supposed to map the result of my requete to the type T , and in the T class, i've tried a lot of type for the field wich is mapped to the tinyint – E.Brudy Jan 17 '17 at 15:31
  • [Possibly related question](http://stackoverflow.com/questions/7410866/dapper-is-throwing-an-invalid-cast-exception-when-trying-to-set-a-boolean-value) – Bridge Jan 17 '17 at 15:32
  • @Bridge this question is related but doesn't help, I've tried to create a backing field and parse the value in int but I have always the same error – E.Brudy Jan 17 '17 at 15:41

1 Answers1

0

Null-able byte? should work.

public class Foo
{
    public byte? Bar { get; set; }
}

    [Test]
    public void TestTinyint()
    {
        var result = _sqlConnection.Query<Foo>(@"select 0 as 'Bar' 
                                                union all select null as 'Bar'");
        Assert.That(result.First().Bar, Is.EqualTo(0));
        Assert.That(result.Last().Bar, Is.Null);
    }

It could be related to MySQL connector issue discussed here.

Void Ray
  • 9,849
  • 4
  • 33
  • 53