如何本地化实体框架内置验证消息/异常? [英] How can I localize the Entity Framework build-in validation messages/exceptions?

查看:192
本文介绍了如何本地化实体框架内置验证消息/异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在基于注释的验证机制旁边,EF提供了所谓的Facets。对字符串属性的含义,您可以有一个最大长度构面(通过EF模型配置),其中指定最大长度为80个字符。



如果现在属性包含的字符数超过指定的字符数,EF将抛出一个DbEntityValidationResult消息


[PropName]:字段[PropName]必须是最大长度为80的字符串或数组类型。 / p>

我的问题:如何本地化此类邮件?我需要它在德国和意大利以及...

解决方案

Maarten Balliauw写了一篇博客博客: p>



更新您的类以使用 ErrorMessageResourceType ErrorMessageResourceName 参数,而不是通常传递的ErrorMessage参数。



可以本地化你的DisplayNameAttributes;这里是一个Stackoverflow的文章,其中解释了如何做: DisplayNameAttribute的本地化



更新



我认为如果安装.NET Framework语言包,这些标准消息应该为您翻译。



一个警告(发生在我身上):


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

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