c# - 哪种方法更好地定义数据注释(视图模型或实体)?
问题描述
在实体或视图模型中定义数据注释的更好方法?
我有同一实体的实体和视图模型,并使用实体框架的数据注释覆盖该类。问题是我可以在不创建单独模型的情况下添加它们,但是我担心哪种方法更好?
先感谢您。
实体
[Table("BatchInfo")]
public partial class BatchInfo
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int BatchInfoId { get; set; }
public string TRANCODE { get; set; }
public string BatchId { get; set; }
public bool AMTFLAG { get; set; }
public int POS { get; set; }
}
查看模型
[NotMapped]
public class BatchInfoViewModel:BatchInfo
{
[Required(ErrorMessage = "Transaction Code is required")]
[Remote("CheckTranCode", "BatchInfo",AdditionalFields = "BatchInfoId", ErrorMessage = "Transaction Code Already Exists!")]
[RegularExpression("[A-Za-z0-9^]*", ErrorMessage = "Invalid Transaction Code")]
[StringLength(3, ErrorMessage = "Transaction Code Should Not be More than 3 Characters")]
public new string TRANCODE { get; set; }
[Required(ErrorMessage = "Batch Code is required")]
[RegularExpression("[A-Za-z0-9^]*", ErrorMessage = "Invalid Batch Code")]
[StringLength(5, ErrorMessage = "Transaction Code Should Not be More than 5 Characters")]
public new string BatchId { get; set; }
[Required(ErrorMessage = "Position is required")]
[RegularExpression("^[0-9]*$", ErrorMessage = "Only Number is allowed")]
[Range(0, int.MaxValue, ErrorMessage = "Invalid Position")]
public new int? POS { get; set; }
}
解决方案
推荐阅读
- javascript - 无法在 javascript 中正确显示
- arrays - 在 Hive 中,这种模式如何从 json 数组中识别嵌套的 json?
- python - 在 CSV 上写入和过去 - Selenium , Python
- python - numpy 数组中的条件
- excel - 如何使用 MS Word 中的宏清除书签数据
- entity-framework - 在 ASP.NET MVC EF 核心中将 50 条记录从 JSON 文件插入/更新到 SQL 表占用太多
- python - 机器人框架的自定义 Python 关键字 - 预期 0 个参数,得到 1 个
- c# - 如何根据用户的 SQL Query 输入动态过滤 ASP.NET 中的 DataTable
- vert.x - Quarkus - 如何使用多个反应式 PgPool 测试 CDI 服务
- git - 将相同的手术修复应用于两个非常不同的分支