问题
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...
回答1:
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
来源:https://stackoverflow.com/questions/12892377/how-can-i-localize-the-entity-framework-build-in-validation-messages-exceptions