How can I localize the Entity Framework build-in validation messages/exceptions?

会有一股神秘感。 提交于 2019-12-12 16:31:25

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!