Microsoft.EntityFrameworkCore.Sqlite C# ulong (64 bit integer) could not be translated (LINQ query)

vb 276 Reputation points
2021-01-18T16:51:47.347+00:00

Hi!

I'm testing database code migration from .NET Framework to .NET 5.last.
I'm using .NET 5, Microsoft.EntityFrameworkCore.Sqlite & LINQ query. (all last varsions)

Following pseudo-code fails when using correspondence C#: ulong type <==> SQLite: BIGINT type ... where...

Entity table is...

public class People
{
      [Key]
      public ulong ID { get; set; }
      public string Name { get; set; }
}

const ulong MaxIDCompactReorderLevel = ulong.MaxValue / 2;

code fails on...

var values = from e in table
                     where e.ID < MaxIDCompactReorderLevel
                     orderby e.ID
                     select e;

I get following error as a result:

The LINQ expression 'DbSet<People>()
.Where(p => p.ID < 9223372036854775807)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.

It looks like LINQ cannot digest ulong numbers ... like number 9223372036854775807 !?
Also, I have tried small ulong number like 100000 ... it fails!
Now I have tried to compile code from Any CPU to x64 ... hm ... LINQ fails again!
LINQ is not ulong 64-bit number compatible?

Thanks in advance,
Vladimir

P.S. when I fetch using table.FromSqlRaw RAW like ...

string sqlSelectMax = "SELECT * FROM People where ID = (SELECT max(ID) FROM People)";
table.FromSqlRaw(sqlSelectMax).ToList().First<People>() as People).ID;

... this ... succeeds.

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,053 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,469 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,135 questions
0 comments No comments
{count} vote

Accepted answer
  1. Viorel 113.7K Reputation points
    2021-01-18T17:40:11.863+00:00

    Did you also check the long type? According to documentation, Int64, long and SqlInt64 represent the official equivalents of bigint.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Karen Payne MVP 35,286 Reputation points
    2021-01-18T17:58:12.887+00:00

    Hello @vb

    This is a limitation of the SqlLite data provider which does not support BigInt You could look at ValueConverter class.

    1 person found this answer helpful.