EF core, apicontroller validation

William Buchanan 61 Reputation points
2022-12-24T08:05:03.937+00:00

Hi all

Can someone tell me how validation works with ef core in a webapi controller?

I have tried data annotations. They work, but I need additional validation. I tried making metadata classes but they just don’t seem to work.

Is there a new way of doing this, or have I missed a step in getting the metadata classes working?

Thanks

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
689 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,077 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,096 questions
{count} votes

Accepted answer
  1. Karen Payne MVP 35,016 Reputation points
    2022-12-24T10:07:41.567+00:00

    Out of the box with database constraints all that is needed is

    public async Task<IActionResult> OnPostAsync()  
    {  
      
     if (!ModelState.IsValid)  
     {  
          return Page();  
     }  
      
      
     return RedirectToPage("Page to go to on successful post");  
    }  
    

    And then of course couple with attributes

    [Required]  
    [Display(Name = "First Name")]  
    [StringLength(50)]  
    public string FirstName { get; set; }  
    

0 additional answers

Sort by: Most helpful