Background:
I have 200+ legacy VB6 apps which I am converting to C#, using EntityFramework Core as our ORM. Unfortunately a lot of the apps utilize MySQL's zero date (0000-00-00)
. So I need to cater for this and be able to store a zero date in some way. Changing the fundamental way this works in the 200+ apps is not currently an option.
Setup: I can define an entity property which represents the field definition in MySQL eg:
public DateTime ExpiryDate { get; set; }
...
entity.Property(e => e.ExpiryDate)
.IsRequired()
.HasColumnType("datetime")
.HasDefaultValueSql("'0000-00-00 00:00:00'");
This will correctly store a zero date if no value is sent on an insert.
Problem:
Because C# has a minimum date of 0001-01-01
it is not possible for me to explicitly store a zero date. So my question is... Is there a way to set up my entities to get this zero date into and out of the database??
So far:
I have tried using a backing field, defined as a string so that I can manipulate any DateTime.MinValue
to become '0000-00-00'
. This allows me to store the zero date but then causes a casting issue (as you would expect) when trying to retrieve the data:
System.InvalidCastException : Unable to cast object of type 'System.DateTime' to type 'System.String'.
Current packages I am using are:
- EFCore 1.1
- PomeloEntityFrameWorkCore 1.1.2
- MySQL 5.7.18