Efcore: [Microsoft.Data.Sqlite] ColumnSize of text fields is always DbNull.Value

Created on 10 Mar 2019  路  5Comments  路  Source: dotnet/efcore

I'm using myDataTable.Load(myDataReader) to load some results into a DataTable, but this raises a ConstraintException:

Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.

And the specific error I get from myDataTable.GetErrors() is:

Column 'MyTextField' exceeds the MaxLength limit

The column 'MyTextField' is defined in the Sqlite database as a VARCHAR (100).
Upon further digging, I noticed the max length value 100 is not retrieved at all. In fact, the schema returned by myDataReader.GetSchemaTable() has a DbNull.Value for the ColumnSize property.

DataTable schemaTable = myDataReader.GetSchemaTable();
var columnSizeForMyTextField = schemaTable.Rows[3]["ColumnSize"]; //This is DbNull.Value

The same code works fine when I use the NuGet package System.Data.SQLite, since the ColumnSize is retrieved correctly.

Is there any fix for this? My current workaround is adding the DataTable to a DataSet with EnforceConstraints set to false and THEN loading the SqliteDataReader.

Thanks

closed-fixed customer-reported good first issue help wanted type-bug

Most helpful comment

I'll take this one.

All 5 comments

Notes from triage: The column size in the type name doesn't really mean anything for SQLite, which is why it wasn't being parsed. However, if this prevents loading into a data table, then we should fix that, either by parsing the length or by some other mechanism.

As mentioned in https://github.com/aspnet/Microsoft.Data.Sqlite/issues/253#issuecomment-323135101, we can parse ColumnSize, NumericPrecision, and NumericScale from the type name.

The simplest possible fix is for us just to return -1 instead of DBNull.

I'll take this one.

Thank you guys!

Was this page helpful?
0 / 5 - 0 ratings