Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Microsoft.Data.Sqlite Get System.OverflowException When read uint type data from sqlite #30910

Closed
CwjXFH opened this issue May 17, 2023 · 4 comments

Comments

@CwjXFH
Copy link

CwjXFH commented May 17, 2023

I read some data from SQLite will throwing System.OverflowException when the column value is greater than int.MaxValue (e.g. 2950121986), column type in SQLite is INTEGER.

await using var conn = new SqliteConnection(connStr);
var entity = await conn.QueryAsync<InfoEntity>("SELECT Vol FROM Daily_Bargain WHERE ROWID=291397");
public class InfoEntity
{
    public uint Vol { set; get; }
}

I change the Vol type to ulong, which will be ok.

package version
Dapper 2.0.123
Microsoft.Data.Sqlite 7.0.5
.NET 7
@roji
Copy link
Member

roji commented May 17, 2023

@CwjXFH that seems like the right behavior, what other behavior are you expecting here?

@CwjXFH
Copy link
Author

CwjXFH commented May 17, 2023

@roji I use uint type in my application, and store the uint type value in SQLite, but when read it from SQLite, get an overflow exception.

@ajcvickers
Copy link
Member

@CwjXFH This should be reported to Dapper since the stack trace indicates it is an issue in Dapper's mapping.

Unhandled exception. System.Data.DataException: Error parsing column 1 (Vol=2147483648 - Int64)
 ---> System.OverflowException: Arithmetic operation resulted in an overflow.
   at Deserializef173a26d-c8fa-4a16-8150-de553f84efa5(IDataReader)
   --- End of inner exception stack trace ---
   at Dapper.SqlMapper.ThrowDataException(Exception ex, Int32 index, IDataReader reader, Object value) in /_/Dapper/SqlMapper.cs:line 3706
   at Deserializef173a26d-c8fa-4a16-8150-de553f84efa5(IDataReader)
   at Dapper.SqlMapper.QueryAsync[T](IDbConnection cnn, Type effectiveType, CommandDefinition command)
   at Program.<Main>$(String[] args)
   at Program.<Main>$(String[] args)
   at Program.<Main>(String[] args)
await using var conn = new SqliteConnection("Data Source=test.db");
var entity = (await conn.QueryAsync<InfoEntity>("SELECT Id, Vol FROM InfoEntity WHERE ROWID=1")).Single();
Console.WriteLine(entity.Vol);

// using (var context = new SomeDbContext())
// {
//      // context.Database.EnsureDeleted();
//      // context.Database.EnsureCreated();
//      //
//      // context.Add(new InfoEntity { Vol = (uint)int.MaxValue + 1 });
//      // context.SaveChanges();
// }
//
// using (var context = new SomeDbContext())
// {
//     Console.WriteLine(context.Set<InfoEntity>().Single().Vol);
// }

public class InfoEntity
{
    public int Id { get; set; }
    public uint Vol { set; get; }
}

public class SomeDbContext : DbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder
            .UseSqlite("Data Source=test.db")
            .LogTo(Console.WriteLine, LogLevel.Information)
            .EnableSensitiveDataLogging();

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<InfoEntity>();
    }
}

@CwjXFH
Copy link
Author

CwjXFH commented May 29, 2023

I got it, thanks!

The dapper team seems to have fixed this issue: DapperLib/Dapper#1795

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants