I seem to have a problem with EF core and my MariaDb database. First of all, I'm unable to do the obvious move to change the database a bit, so that option is off the table.
I have a table "Contacts" where a contact date is being kept. This contact date is being kept in the form of a string value, containing only the date, as such: 2020-08-30 for 30th august of 2020. I have an EF core mapping for this field as such:
entity.Property(e => e.ContactDate)
.IsRequired()
.HasColumnName("contactDate")
.HasColumnType("varchar(255)")
where e.ContactDate is an DateTime property.
When I use the property in my code, the datetime works as expected and contains the date saved in the databank. hooray!
The problem however arises when I want to query on that datetime. Given this query:
SELECT `c7`.`contactDate`
FROM `contacts` AS `c7`
WHERE (`f`.`uuid` = `c7`.`fileUuid`) AND (`c7`.`numberOfContacts` > 0)
ORDER BY `c7`.`contactDate`
LIMIT 1) <= @__endDate_1) AND (`f`.`closingDate` IS NULL OR ((`f`.`closingDate` >= @__startDate_2)
The properties startDate and endDate are inserted into the query as parameters. For example, @__endDate_1='2019-12-31 00:00:00'
. And here lies the problem: MySql is going to string compare the contactDate db field with the string value of that endDate. Because one has a time field and the other one does not, I run into problems if I want to do a greather than function. (SELECT "2020-02-04" >= "2020-02-04 00:00:00"
returns 0).
Is there a way that i can:
- Or change EF that he casts the contactDate to a value with a timestamp in every query
- Or change EF that he casts the DateTime value to a Date Value without any time values?
What do you guys feel about a possible third solution where I introduce a new "Date" class as a DateTime to Date string wrapper for EF?
Thanks!