0

Using Entity Framework Core (Code First) and SQLite, Guid is stored as binary but Decimal and Date fields are stored as text with Microsoft's provider.

I can understand they might not want the imprecision of DOUBLE for currency amounts and thus use text.

What happens if I need to sort? Is Entity Framework Core smart enough to make sorting work as expected (but slower because it needs to parse everything!), or will it sort alphabetically instead of sorting by number? I don't want it to return 100 before 2.

I'll have to do things like "give me the latest order" so what's the best approach for that? I want to make sure it's going to work.

Am I better to switch to System.Data.SQLite provider to store dates in UNIX format (this is not supported by the Microsoft's provider)? and then would I have to do the parsing back and forth myself or it could take care of it automatically?

Etienne Charland
  • 3,424
  • 5
  • 28
  • 58
  • Check out this thread: [Sorting dates in sqlite database?](https://stackoverflow.com/questions/13300463/sorting-dates-in-sqlite-database) – Jacob H Apr 10 '19 at 15:49
  • Actually it stores as "2019-04-10 15:35:17.8548953Z" so I guess sorting should work on strings. However we can't say the same for decimal stored as "1" and "200" – Etienne Charland Apr 10 '19 at 15:51
  • See the [SQLite query limitations](https://learn.microsoft.com/ef/core/providers/sqlite/limitations#query-limitations) of the EF Core docs. – bricelam Sep 16 '19 at 16:44

1 Answers1

1

I am still learning system.data.sqlite myself, but I am aware that you can create and assign a custom collation to your column. The collation can either be assigned to the table column, or only to a particular view or query using standard sqlite SQL syntax and the COLLATE keyword.

This is not a complete example/tutorial, but for starters visit the Microsoft.data.sqlite docs. Also see this stack overflow answer. These are just hints, but provide a consistent method to do this. Remember that sqlite is an in-process DB engine, so it should still be rather efficient and still allow working with the database in a normal fashion without having to constantly inject custom logic between queries. Once you have the custom collation defined and properly registered, it should be rather seamless with perhaps the only extra requirement to append e.g. COLLATE customDecimal to the ORDER BY clauses.

The custom collation function would convert the string value to an appropriate numeric type and return the comparison. It's very similar to the native .Net IComparer and IComparison interfaces/implementations.

C Perkins
  • 3,733
  • 4
  • 23
  • 37