EF的自定义ValidationContext? [英] Custom ValidationContext for EF?
问题描述
我正在使用EF作为数据提供者来保存和验证我的物品。
我有一些自定义验证逻辑,需要访问其他服务或项目。
我知道ValidationContext类提供了这些选项。
我的问题是如何通过服务提供商或项目将自己的验证上下文获取到EF?
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?
当我在POCO实体上实现IValidateObject接口时,获取验证上下文的实例,但是在哪里可以确保使用我自己的ValidationContext而不是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?
我想基于这一事实进行验证确认服务或项在ValidationContext中可用
I want to base some validation on the fact that a service or item is available in the ValidationContext
推荐答案
要传递ValidationContext,您需要重写DbContext.ValidateEntity()。该方法采用两个参数-实体条目和项目。您将在项目字典中传递您的验证上下文。看看这个stackoverflow问题。
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.
这篇关于EF的自定义ValidationContext?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!