F #, Entity Framework, and NULL Values

This is a very complex problem in F #, which is extremely simple in C #

I am using a custom DomainManager to write the Azure Mobile application table controller in F #. DomainManager inherits from MappedEntityDomainManager<,> , which uses Automapper under covers to map my database objects to mobile reports.

The Automapper profile is as follows:

 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 valid DateTime , and since its class inherits from EntityData , dest.UpdatedAt is a valid DateTimeOffset .

This code compiles, but when I try to run it, I get an error

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 storage expression. '

This seems to apply to my use of the .Value Nullable<DateTime> property. The main reason is strict type checking F #. In C #, would this be easily handled by a null coalescing operator ?? .

Is there a way to simulate zero coalescence as part of an Entity Framework-friendly expression in F #?

+7
f # entity-framework
source share
1 answer

It looks like you are trying to pass a method in the Entity Framework. Download the result first, and then apply the mapping. This is a common mistake, as answered here: NotSupportedException: LINQ to Entities does not recognize the method

0
source share

All Articles