mapperly icon indicating copy to clipboard operation
mapperly copied to clipboard

Nullable collections in queryable projection mappings cannot be translated by EF Core

Open latonz opened this issue 2 years ago • 6 comments

Mapperly adds non-null conditions when mapping properties if the property is nullable or in a disabled nullability context. If this happens inside a queryable projection mapping, EF Core is not able to translate these non-null assertions for navigational collection properties. Mapperly should special-case this and not generate the not-null condition on collection properties inside queryable projection mappings.

Brought up by @Seszele in https://github.com/riok/mapperly/discussions/855

latonz avatar Nov 04 '23 11:11 latonz

For me, I think null check on source target properties should be enabled. It will help in cases when Child Collections are not included in Entities.

nabeel-hayya avatar Nov 29 '23 13:11 nabeel-hayya

This would also be EF Core specific, although IQueryable may be used by other components... Need to think about this, we probably need to have a configuration which ensures EF Core compatibility for IQueryable projection mappings. Even then it would be quite hard to find out at compile time whether a given collection/property is included in the EF Core model.

latonz avatar Dec 02 '23 14:12 latonz

It would be hard, But that's why devs are looking for "Mapperly"

inabeel avatar Dec 03 '23 10:12 inabeel

Hi, is there any development on this issue? I am struggling with this problem and I hope it will be solved soon :)

RezaBagheri8 avatar Feb 20 '24 07:02 RezaBagheri8

No there are no news on this one.

latonz avatar Feb 22 '24 04:02 latonz

May I know why do you use nullable collection properties?

According to MS (https://learn.microsoft.com/en-us/ef/core/miscellaneous/nullable-reference-types):

Collection navigations, which contain references to multiple related entities, should always be non-nullable. An empty collection means that no related entities exist, but the list itself should never be null.

Also see https://github.com/dotnet/efcore/issues/29892. Here the conclusion is they usually should not be null, although there are some rare cases where nullable collections make sense. I'm interested in what your use-case actually is.

latonz avatar Feb 22 '24 04:02 latonz

Closing for now as no further feedback is provided.

latonz avatar Mar 11 '24 07:03 latonz