This is an extremely difficult problem in F#, that is extremely easy in C#
I'm using a custom DomainManager
to write an Azure Mobile App Table Controller in F#. The DomainManager
inherits from MappedEntityDomainManager<,>
, which uses Automapper under the covers to map my database objects into mobile-friendly records.
The Automapper profile looks like this:
type EventOrganiserProfile() =
inherit Profile()
do
let nullDateTime = Nullable<DateTime>()
base.CreateMap<MyDbEntity, MyMobileDto>()
...
.ForMember(fun dest -> dest.UpdatedAt, fun opt -> opt.MapFrom(fun src -> if src.CancelledUtc <> nullDateTime then src.CancelledUtc.Value else src.StartedUtc))
...
|> ignore
base.CreateMap<MyMobileDto, MyDbEntity>()
...
|> ignore
src.StartedUtc
is a DateTime
, src.CancelledUtc
is a nullable DateTime
, and because its class inherits from EntityData
, dest.UpdatedAt
is a nullable DateTimeOffset
.
This code compiles, but when I try to run it, I get the error message
System.NotSupportedException: 'LINQ to Entities does not recognize the method 'System.DateTime Invoke(System.Nullable`1[System.DateTime])' method, and this method cannot be translated into a store expression.'
What this seems to be referring to is my use of the .Value
property of Nullable<DateTime>
. The underlying cause is F#'s rigorous type-checking. In C# this would have been handled seamlessly by the null coalescing operator ??
.
Is there a way to simulate null coalescing as part of an Entity Framework-friendly expression in F#?