I make queries to MySQL database through EF Core to get entries. Some of these entries have zero dates
0000-00-00 00:00:00
which raises the famous MySql.Data.Types.MySqlConversionException
exception:
'Unable to convert MySQL date/time value to System.DateTime'
I tried to solve this by adding to my connection string the Allow Zero Date
flag (in all the different ways possible) - didn't work.
Then I tried to write an interceptor that overrides ReaderExecuted method:
class DateTimeInterceptor : Microsoft.EntityFrameworkCore.Diagnostics.DbCommandInterceptor
{
public override DbDataReader ReaderExecuted(DbCommand command, CommandExecutedEventData eventData, DbDataReader result)
{
var columns = result.GetColumnSchema();
for (int columnIndex = 0; columnIndex < columns.Count; columnIndex++)
{
if (columns[columnIndex].DataType.Name == "DateTime")
{
var columnName = columns[columnIndex].BaseColumnName;
}
}
// TODO: check the datetime column values and if needed replace them
return base.ReaderExecuted(command, eventData, result);
}
Which kinda can work, if I manually read in correct way all the model data, copy it to another reader and pass as a result. That doesn't feel right though, I have 11 models and I don't want to implement manually the readers for all of them - too stupid.
So, what else can I do here? Basically what I need is to search and replace values of certain fields before accessing them.
And here is how these fields are defined in my model class:
[Column("created_at")]
public string CreatedAt { get; set; }
[Column("updated_at")]
public string UpdatedAt { get; set; }
There is this solution, but it doesn't look very neat either...