实体框架验证 [英] Entity Framework Validation
问题描述
我以前没有使用过实体框架,但快速搜索表明您有几个选项
1)在应用程序的另一层验证
总是一个选项,我只是以为我会抛出
2)挂接到实体的OnChanged事件然后执行验证
可能是脆弱的并且会变得混乱/缓慢,如果你有许多不同的属性,可以改变每个实体。
3)实现部分方法验证属性更改
根据这个信息和这个演练有部分方法可用用于验证。这似乎是您最好的选择,因为它不是非常干扰,您可以选择性地实施所需的验证。
我希望有所帮助。祝你好运。
I'm getting ready to start a new project and I've been researching the entity framework. My question is what is the best strategy for validating the entities? Other projects I've worked on have used attributes for most of the validation, but obviously this is not possible in the entity framework. Is the only way to do this by handling the partial methods in the property setters? All advice is much appreciated.
I have not actually used the Entity framework before but a quick search indicates that you have several options.
1) Validate at another layer in your application
Always an option, I just thought I would throw it out there explicitly.
2) Hook into the OnChanged events of the Entity then perform validation
Likely brittle and would become confusing/slow after if you have many different properties things that can change for each entity.
3) Implement partial methods to validate property changes
According to this post and this walkthrough there are partial methods available for validation. This seems like your best option as it is not very intrusive and you can selectively implement the validation you want.
I hope that helps. Good luck.
这篇关于实体框架验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!