我正在开发一套WebAPI。如果我使用DataAnnotation定义我的模型:
public Prat()
{
public int Id { get; set; }
[Required]
[StringLength(10)]
public string Pratica { get; set; }
public int Anno { get; set; }
}当我像这样发送帖子(使用Postman)时:
{
"pratica": "",
"anno": 2000,
}不出所料,我得到了这个错误
{
"Pratica": [
"The Pratica field is required."
]
}但是,如果我使用FluentApi而不是DataAnnotation:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Prat>(entity =>
{entity.Property(e => e.Pratica)
.IsRequired()
.HasColumnType("varchar(10)")}
}不执行验证。为什么?谢谢
发布于 2017-05-30 17:37:48
这是因为Data Annotation同时执行验证和映射,而FluentAPI只负责映射。
https://stackoverflow.com/questions/44257826
复制相似问题