I am using EF in a multi-language site to map results from SPs to objects.
ctx.Database.SqlQuery<MyDTO>("MySP {0}", Id).SingleOrDefault()
I see that there is an issue with mapping dates when the culture changes.
More specifically, I get the same date either as 16/12/2015 09:06:15
or 12/16/2015 09:06:15
, depending on the user culture.
I know two solutions to this:
- Get the date as a string and parse it with
CultureInfo.InvariantCulture
. - Switch the culture to
CultureInfo.CultureInvariant
before calling the repository methods.
And perhaps there is another option:
- I see that we change both:
Thread.CurrentThread.CurrentUICulture
andThread.CurrentThread.CurrentCulture
to the user's locale, but I think we should only switch the UI one. But I am not sure what will break if I change this...
Is there any alternative, like setting the culture on the EF context?
Update:
Chaning the Thread.CurrentThread.CurrentUICulture
and Thread.CurrentThread.CurrentCulture
just before doing the query doesn't seem to help either. That is confusing... Perhaps EF caches the culture at an earlier point?