ASP.NET and EF Core - ModelState.IsValid always returns true for models generated by Scaffold-DbContext

user7003875

I've been unable to validate models generated by the Scaffold-DbContext command in my ASP.NET Core controllers.

The required/max length property configurations are all in the onModelCreating method of the context class that EF core generated.

protected override void OnModelCreating( ModelBuilder modelBuilder ) {
    modelBuilder.Entity<ModelClass>( entity => {
        entity.ToTable( "ModelClass", "schema" );

        entity.Property( e => e.ModelClassCode )
              .IsRequired()
              .HasMaxLength( 30 );

My controller receives the data for the models as JSON but ModelState.IsValid always returns true even if I send invalid data.

public IActionResult CreateModelClass( [FromBody]ModelClass modelClass ) {
    // ModelState.IsValid always returns true here

The only way I've gotten ModelState.IsValid to be false is by adding data annotations to the model class. I'd like to avoid that because running the scaffold command will overwrite those changes.

Am I missing something here? Is there an example that someone can point me to? The ones I've seen related to validation are all using data annotations.

user7003875

As mentioned in @Smit's comment, you can use the -DataAnnotations switch in the Scaffold-DbContext command to generate the models with the correct validation annotations.

See the documentation for Scaffold-DbContext here: https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/powershell

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

ASP.NET 5, MVC6, WebAPI -> ModelState.IsValid always returns true

ModelState.IsValid always returns false in ASP.NET Core 6.0 MVC

VB.NET - MVC ModelState.IsValid is always true

ModelState IsValid returns False Using ASP.NET Core 5 MVC Identity with UserName

Why ModelState.IsValid always return true?

EF Core ModelState.IsValid Always return False , Because navigation property is null

ASP.NET MVC 5 ModelState.IsValid is always returning false in MVC

ModelState is always valid in ASP.NET.CORE 2.2 Web Api

.Net 6 EF Core DbContext and Models In Separate Class Libary

ModelState.IsValid always evaluating as true when working with FluentValidation

ModelState.IsValid always true when accessed on View

ModelState.IsValid keeps being false because of SelectList in ASP.NET Core

ASP.NET MVC Core 3.0 - Why API Request from body keeps returning !ModelState.IsValid?

Knockout validation plugin - isValid( ) always returns true

printDialog.PrinterSettings.IsValid returns true always

ModelState.IsValid includes a navigation property. Always false. (only net-6.0) How to get true?

Override EF Core DbContext in ASP.NET Core WebApplicationFactory

Asp.Net Core and Scaffold

How to update dbcontext scaffolding in ASP.NET 5 with EF Core?

In .Net Core API, ModelState.IsValid is not catching Required parameters

ModelState.IsValid is true, but why?

What is ModelState.IsValid valid for in ASP.NET MVC in NerdDinner?

How ModelState.IsValid works in asp.net webapi

Scaffold-DbContext Stored Procedures only in EF Core

EF Core Scaffold-DbContext table not included in the selection set

Scaffold-DbContext for database views in EF Core 2.1 (Query Types)

EF Core 2.0 scaffold-dbcontext Find ConnectionString in another project

ASP net Core Razor Pages - split models for EF and views

why does Powershell Test-Path -Isvalid always returns true