3 回答
TA贡献1877条经验 获得超6个赞
进一步扩大答案。通过使用Microsoft Patterns&Practices企业库5验证模块,除了常规数据注释可用的方法之外,您还可以打开许多验证方法。
using Microsoft.Practices.EnterpriseLibrary.Validation;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;
[HasSelfValidation]
public partial class Category : ICategory
{
[SelfValidation]
public void Validate(ValidationResults validationResults)
{
if (this.Title === "Credo")
{
validationResults.AddResult(
new ValidationResult(
"Category title cannot be a veiled reference to a former cool 2000AD character.",
this,
null,
null,
null));
}
validationResults.AddAllResults(
ValidationFactory
.CreateValidator<ICategory>()
.Validate(this));
}
}
using System;
using System.ComponentModel.DataAnnotations;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;
public interface ICategory
{
int Id
{
get;
set;
}
[Required]
[StringLengthValidator(1, 50, MessageTemplate = "Category title should be a maximum of 50 characters in length.")]
string Title
{
get;
set;
}
}
TA贡献1820条经验 获得超10个赞
生成的POCO上的属性是从模型中实体的各个方面派生的。例如,用于[Required]
确保该字段为“非空”,并且用于[StringLength(n)]
确保数据类型nvarchar(n)
通过MaxLength
构面。
- 3 回答
- 0 关注
- 543 浏览
添加回答
举报