2017-05-30 9 views
0

私はWebAPIのセットを開発しています。 私はDataAnnotationを使用して、私のモデルを定義する場合:FluentApi IsRequiredがPostManによって無視される

public Prat() 
{ 
    public int Id { get; set; } 
    [Required] 
    [StringLength(10)] 
    public string Pratica { get; set; } 
    public int Anno { get; set; } 
} 

私はこのようなPOST(ポストマンを使用して)送信する場合:私はこのエラーを得た

{ 
    "pratica": "", 
    "anno": 2000, 
} 

、期待通りの

{ 
    "Pratica": [ 
    "The Pratica field is required." 
    ] 
} 

しかし、 、DataAnnotationの代わりにFluentApiを使用する場合:

protected override void OnModelCreating(ModelBuilder modelBuilder) 
{ 
    modelBuilder.Entity<Prat>(entity => 
      {entity.Property(e => e.Pratica) 
        .IsRequired() 
        .HasColumnType("varchar(10)")} 
} 

検証は実行されません。 なぜですか? ありがとうございました

答えて

0

これは、Data Annotationが検証とマッピングの両方を実行しているのに対して、FluentAPIはマッピングの責任を負うためです。

See this answer for more info

+0

ありがとうグレゴリー! – skysurfer

関連する問題