ef core saves the null value of the required property

Is it an EF Core issue or I am missing something in model mapping?

It is indeed EF Core issue, which also is observed in the EFC 5.0 release candidate, so it won’t be addressed (except if you change the database design to utilize TPT).

First, unlike EF6, EF Core does not perform validations in general. Instead, it relies on the underlying database to do that.

Second, since the TPH database inheritance strategy stores all data in a single table, the derived entity data columns must allow null, even though they are required, otherwise you won’t be able to store Teacher entity data for instance.

The combination of the two aforementioned behaviors leads to the unexpected behavior of allowing null value for the required field of a derived entity.

Therefore there is nothing you could do at mapping side (neither fluently nor with [Required] data annotation). The necessary validations should be performed by the business logic layer outside the EF Core.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top