entity-framework - 覆盖 .NET Core 中的默认 Required ErrorMessage 属性
问题描述
我已经使用带有 DataAnnotations 集的 EF 生成了我的 DbContext,并希望使用 ModelMetadataType 覆盖所需的 ErrorMessage 属性
例如
生成的类
public partial class ControlType
{
public ControlType()
{
Rule = new HashSet<Rule>();
}
[Key]
public int ControlTypeId { get; set; }
[Required]
[StringLength(200)]
public string Name { get; set; }
[InverseProperty("ControlType")]
public virtual ICollection<Rule> Rule { get; set; }
}
我的扩展类
[ModelMetadataType(typeof(ControlTypeMeta))]
public partial class ControlType
{
}
public class ControlTypeMeta
{
[Display(Name = "Id")]
public int ControlTypeId { get; set; }
[Display(Name = "Control type")]
[Required(ErrorMessage = "'{0}' is required")]
public string Name { get; set; }
}
有了上面的内容,我期望将所需的错误消息输出为“需要'控制类型'”,但我得到默认的“需要控制类型字段”
有谁知道我可以如何覆盖消息(不创建视图模型)
解决方案
推荐阅读
- c# - 无法访问或实例化新创建的类
- pointers - 为什么在 Go 中使用自定义 http.Handler 时使用指针?
- javascript - 如何让 Javascript 在移动到另一个函数之前等待一个函数完成?
- aspnetboilerplate - AspNetBoilerplate 未从数据库加载设置
- php - PHP JSON数组对象添加0作为键
- c# - 在 url 中传递名称而不是 id 会引发错误
- reactjs - 有没有办法使用反应导航在标题中插入图像+文本并将其居中?
- google-apps-script - 从 Google 脚本将数据插入 BigQuery:遇到“”
- c# - .ToDictionary KeyValuePair 将键/值重命名为名称/值 json.net 输出
- c# - 在 WPF 应用程序中显示 DirectX (SharpDX.Windows.RenderForm)