Z.EntityFramework.Plus.EFCore - Official behavior on DataAnnotations ?
Description
We are using the commercial version of Z.EntityFramework.Plus.EFCore and using a lot of
- BulkInsertAsync()
- BulkUpdateAsync()
- BulkDeleteAsync()
- BulkSynchronizeAsync()
We were under the impression that ZEntity was validating each model and their DataAnnotation before saving, like EF Core does. We had an edge case where the database column would allow NULL (for legacy purpose) but the EF Model was declared as
[Required] // Here we want to simulate a DB that allows null but marked as required on the model
public int? NullableInt { get; set; }
To our surprise, the Bulk methods would allow invalid values (null) to reach the database. I just want to validate your official position regarding DataAnnotations. Should we validate each object manually before calling Bulk() methods or is that something that should be handled by ZEntity Bulk() methods ?
Thanks for your time!
Further technical details
- .NET Core 8
- EF Core v8.0.5
- Z.EntityFramework.Plus.EFCore: [v8.102.2.4]
- Database Server version: [SQL Server 2022]
Hello @jsparent ,
You are right. At this moment, we are not validating the [Required] data annotation for the Bulk Insert method. It simply inserts the data.
However, the BulkSaveChanges will act like the SaveChanges (looking at [Required] data annotation.
At this moment, there is no plan to change this behavior but we might eventually in the future add an option for this.
So at this moment, you will need to check if the value is null or not on your side.
Let me know if that answer correctly to your question.
Best Regards,
Jon
Alright, thanks for the info, very clear!
In your opinion, do you think using https://entityframework-extensions.net/pre-bulk-insert and https://entityframework-extensions.net/pre-bulk-update would be a good choice to implement object validation if we want to ensure it is called every time?
Hello @jsparent ,
It depends if you use IncludeGraph or not.
The PreBulkInsert event returns you the list (obj) you provided, not every entity in the graph, so you will need to handle the graph on your side.
So, if you don't use the IncludeGraph option, it's indeed a very good place to handle it.
Depending on what exactly you are doing, perhaps creating your own method, such as EnsureDataIsValid<T>(List<T> list) and calling it before any use of our library might also be a good idea.
It will offer you more control over our PreBulk[Action] event, but it still works.
Best Regards,
Jon
Thanks Jon, as we try to stay away as much as possible from the IncludeGraph option, we will try the pre-bulk event approach. Thank you for your insights.