如何本地化实体框架内置的验证消息/异常? [英] How can I localize the Entity Framework build-in validation messages/exceptions?
问题描述
除了基于注释的验证机制EF提供了所谓的Facet。对于字符串属性的含义,您可以通过最大长度方面(通过EF模型配置)指定最大长度为80个字符。大多数情况下,当您使用数据库第一种方法时,这是由EF自动完成的。
如果现在该属性包含多于指定数量的字符,则EF将抛出一个DbEntityValidationResult消息
[PropName]:字段[PropName]必须是最大长度为80的字符串或数组类型。 / p>
我的问题:我如何本地化这样的消息?我会需要德语和意大利语。
Maarten Balliauw写了一篇博客博客: p>
本地化ASP.NET MVC 2 DataAnnotations验证消息
更新您的类以使用 ErrorMessageResourceType
和 ErrorMessageResourceName
参数,而不是您通常通过的ErrorMessage参数。
您可以本地化您的DisplayNameAttributes;这里是一个Stackoverflow文章,解释了如何做: DisplayNameAttribute的本地化
更新
我认为如果您安装.NET Framework语言包,则应为您翻译这些标准消息。一个警告(发生在我身上): ASP.NET MVC 3本地验证消息在我的机器上工作,但不在服务器上
Beside the annotation-based validation mechanism EF provides the so-called "Facets". Meaning on a string property you could have a "Max Length" Facet (through the EF model configuration) where you specify a max-length of 80 chars. Most often this is done automatically by EF when you use a database first approach.
If now the property contains more than the specified number of chars, EF will throw a DbEntityValidationResult with the message
[PropName]: The field [PropName] must be a string or array type with a maximum length of '80'.
My question: How can I localize such message??? I'd need it in german and italian as well...
Maarten Balliauw wrote a blog blog about it:
Localize ASP.NET MVC 2 DataAnnotations validation messages
Update your classes to use the ErrorMessageResourceType
and ErrorMessageResourceName
parameters instead of the ErrorMessage parameter that you normally pass.
Also, you can localize your DisplayNameAttributes; here's a Stackoverflow post which explains how to do it: Localization of DisplayNameAttribute
Update
I think if you install the .NET Framework Language Pack these standard messages should be translated for you.
A caveat (happened to me): ASP.NET MVC 3 localized validation messages work on my machine, but not on server
这篇关于如何本地化实体框架内置的验证消息/异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!