实体框架中的数据注释不起作用

编程入门 行业动态 更新时间:2024-10-26 22:16:04
本文介绍了实体框架中的数据注释不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经在实体框架的edmx文件后面的代码中使用了数据验证,如下所示: [Range(1,5,ErrorMessage =输入1到5之间的值")]

Hi, I have used a data validation in the code behind of edmx file of entity framework as: [Range(1, 5, ErrorMessage = "Enter a value between 1 and 5")]

public global::System.Decimal UnitPrice { get { return _UnitPrice; } set { OnUnitPriceChanging(value); ReportPropertyChanging("UnitPrice"); _UnitPrice = StructuralObject.SetValidValue(value); ReportPropertyChanged("UnitPrice"); OnUnitPriceChanged(); } }

当我在使用方应用程序上使用此实体时,它将不检查范围验证.

When I use this entity on consuming application then it does not check the range validation.

推荐答案

要实现此目的,您必须为中的每个实体类创建一个第二部分类EF并将其链接到具有替代属性的辅助类. 例如,您有一个生成的 To achieve this you have to create a 2nd partial class for each entity class in EF and link it to a auxiliary class with substitute properties. for example you have a generated class Product { public int UnitPrice{ get; set; } }

生成的类将始终标记为局部类. 现在您必须创建第二个部分类文件并添加

The generated class will always be marked as partial. and now you have to create 2nd partial class file and add

[MetadataType(typeof(ProductMetadata))] public partial class Product { // it's possible to add logic and non-mapped properties here } public class ProductMetadata { [Range(1, 5, ErrorMessage = "Enter a value between 1 and 5")] public int UnitPrice { get; set; } // note the 'object' type }

会起作用的. 如果觉得有用,请对其评分.

It will work. Rate it if you find it as useful.

更多推荐

实体框架中的数据注释不起作用

本文发布于:2023-11-12 13:41:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581657.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:注释   实体   框架   不起作用   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!