Entity Framework and System.Data.Sqlite Datetime issue

I had a problem displaying an existing sqlite3 database (on top of system.data.sqlite) with entity code (version 5).

There is a table in the database called notes, which I map to the entity class:

public class Note { [Column("ZNAME") public string Name { get; set; } [Column("ZDATE")] public DateTime Date { get; set; } [Column("ZNOTE")] public string Description { get; set; } } 

In the database, for example, I have 2 rows, one ZDATE field is empty, the other has some date (example: 30/12/1899 21:00:05). When I unit test it, and trying to get the whole collection or this first row with an empty datetime field, I get the notorious exception: "String was not recognized as a valid DateTime." Trying to get only another string (with date), my test passes.

At first, I thought changing a DateTime to a string would solve the problem, but that gives me the same exception. I tried using DateTime ?, same error.

It looks like maybe I'm wrong that System.Data.Sqlite is trying to convert this field to datetime (due to name or something else).

This is the stacktrace of my exception:

 at System.DateTimeParse.ParseExactMultiple(String s, String[] formats, DateTimeFormatInfo dtfi, DateTimeStyles style) at System.DateTime.ParseExact(String s, String[] formats, IFormatProvider provider, DateTimeStyles style) at System.Data.SQLite.SQLiteConvert.ToDateTime(String dateText, SQLiteDateFormats format, DateTimeKind kind) at System.Data.SQLite.SQLiteConvert.ToDateTime(String dateText) at System.Data.SQLite.SQLiteConvert.ToDateTime(IntPtr ptr, Int32 len) at System.Data.SQLite.SQLite3.GetDateTime(SQLiteStatement stmt, Int32 index) at System.Data.SQLite.SQLite3.GetValue(SQLiteStatement stmt, Int32 index, SQLiteType typ) at System.Data.SQLite.SQLiteDataReader.GetValue(Int32 i) at System.Data.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader`1.GetUntypedValueDefault(DbDataReader reader, Int32 ordinal) at System.Data.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader`1.GetValue(DbDataReader reader, Int32 ordinal) at System.Data.Common.Internal.Materialization.Shaper.GetPropertyValueWithErrorHandling[TProperty](Int32 ordinal, String propertyName, String typeName) at lambda_method(Closure , Shaper ) at System.Data.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly[TEntity](Func`2 constructEntityDelegate, EntityKey entityKey, EntitySet entitySet) at lambda_method(Closure , Shaper ) at System.Data.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper) at System.Data.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.MoveNext() at System.Linq.Enumerable.First[TSource](IEnumerable`1 source) 

Can someone give me some idea on how to solve this problem.

+4
source share
2 answers

Do you need to use DateTime? like type

 [Column("ZDATE")] public DateTime? Date { get; set; } 

This will allow you to use null values ​​for the Date field.

0
source
0
source

Source: https://habr.com/ru/post/1445717/


All Articles