2017-05-30 9 views
0

Ich entwickle eine Reihe von WebAPI. Wenn ich definieren mein Modell DataAnnotation mit:FluentApi IsRequired von PostMan ignoriert

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

Wenn ich eine POST senden (mit Postman) wie folgt aus:

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

ich diesen Fehler bekam wie erwartet

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

Aber , wenn ich FluentApi anstelle von DataAnnotation verwende:

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

Die Validierung wird nicht durchgeführt. Warum? Vielen Dank

Antwort

0

Dies liegt daran, dass Data Annotation sowohl die Validierung als auch das Mapping durchführt, während FluentAPI nur für das Mapping zuständig ist.

See this answer for more info

+0

Danke Gregory! – skysurfer