Reputation: 744
I am using a sqlite database. Within this database i have a table which contains a field named CREATED_ON of type TEXT to store the creation timestamp. In my C# code the field is of type DATETIME.
If i try to get all entries via the DBSET normally everything works fine
return context.dbset.ToList();
Unfortunately there is a problem in my code that sometimes a corrupt value (string not parasable to DATETIME) is stored in the CREATED_ON field (At the moment i do not know why this happens). If i try to get all entries i get following error message:
"The 'Created_On' property on 'FileInfos' could not be set to a 'System.String' value. You must set this property to a non-null value of type 'System.DateTime'. "
My question is if there is a possibility to check if the CREATED_ON field includes a value which is parsable to DATETIME or not when getting all entries? Even a foreach loop does not help because every entry is trying to be parsed in the given class before.
EDIT:
Yes the field is straight mapped to DATETIME
builder.Entity<FileInfos>().
Property(i => i.CreatedOn).
HasColumnName("CREATED_ON");
In the FileInfos class:
public DateTime CreatedOn
{
get { return CreatedOnInternal.ToUniversalTime(); }
set { CreatedOnInternal = value.ToUniversalTime(); }
}
Upvotes: 0
Views: 318
Reputation: 668
You can use DateTime.TryParse(input, out variable to output to if correct)
Example:
string input = "2000-02-02";
DateTime dateTime;
if (DateTime.TryParse(input, out dateTime))
{
//only print if the parsing was successful
Console.WriteLine(dateTime);
}
Upvotes: 0