实体框架验证 [英] Entity Framework Validation

查看:94
本文介绍了实体框架验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正准备开始一个新的项目,我一直在研究实体框架。我的问题是什么是用于验证实体的最佳策略是什么?我已经工作过的其他项目使用的属性对于大多数验证,但显然这是不可能的实体框架。是通过处理属性设置器部分的方法来做到这一点的唯一途径?所有的建议是多AP preciated。

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)验证在另一层在应用程序

1) Validate at another layer in your application

始终是一个选择,我只是想我要明确地把它扔在那里。

Always an option, I just thought I would throw it out there explicitly.

2)钩到实体然后执行验证给onChanged事件

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)实现部分的方法来验证属性更改

3) Implement partial methods to validate property changes

根据这个职位和的this 演练有可供验证的部分方法。这似乎是最好的选择,因为它是不是很侵扰,你可以有选择地实现你想要的验证。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆