实体字段长度约束
我目前正在使用实体(使用 Visual Studio 2008,FW 3.5 SP1)。 我从数据库创建了我的实体,一切都很好,除了我对字符串字段的长度限制没有强制执行。 浏览互联网,我发现这是很正常的(参见此 博客文章),并且实体在不检查的情况下将数据发送到数据库,可能会发送异常。
我知道我可以为我的实体编写部分类,并使用 On
所以我的问题是:有没有通用的方法来做到这一点?对于具有最大长度的每种数据类型,是否可以检查提供的值是否合适?
I'm currently working with Entities (using visual studio 2008, FW 3.5 SP1).
I created my entities from the database, and everything is ok, except that my length constraints on the string fields are not enforced.
Browsing the internet, I found out that this is quite normal (see this blog post), and that Entities sends the data without check to the database, to possibly send exceptions.
I know I could write partial classes for my entities, and use the On<Fieldname>Changing
methods, but this would require quite an amount of repetitive code to be written. Moreover
if I change a length in the DB, this would require me to change my code as well.
So my question is: Is there a generic way to to this ? Is it possible, for each data type which has a maximum length to check if the provided value is fitting ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于我没有找到一种通用的方法来做到这一点,所以我最终编写了所有
OnChanging
方法,并制作了一个小型通用验证框架,当某些条件不满足时,它会抛出异常遇见了。As I didn't find a way to do this in a generic way, I finally wrote all
On<Fieldname>Changing
methods, and made a small generic verification framework, which throws exceptions when some criteria is not met.