Custom ValidationContext for EF?

不想你离开。 提交于 2019-12-11 01:46:27

问题


I'm using EF as my dataprovider to save and validate my items. I have some custom validation logic which needs access to other services or items. I know the ValidationContext class supplies these options. My question is how can I get my own validation context with an service provider or item to EF?

When I implement the IValidateObject interface on my POCO entity I get an instance of the validation context, but where can I make sure my own ValidationContext is used instead of the EF default one?

I want to base some validation on the fact that a service or item is available in the ValidationContext


回答1:


To pass the ValidationContext you need to override DbContext.ValidateEntity(). The method takes two parameters - entity entry and items. You would pass your validation context in the items dictionary. Take a look at this stackoverflow question.



来源:https://stackoverflow.com/questions/12918194/custom-validationcontext-for-ef

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!