Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What to use instead DbEntityValidationException in EF Core?

With EF I used DbEntityValidationException catch branch (along with others)

catch (DbEntityValidationException exception)
{
    // any statements here...
    throw;
}

Now using .NET Core 3.17 with EF Core 3.17 and apparently there is no DbEntityValidationException and also reviewed EF Core 3.17 sources, and no traces of that exception type. (Note, I am not only talking about where it is defined, I mean it is not thrown any places in the EF Core 3.17 sources)

Question

How to migrate the code above to EF Core 3.17?

like image 902
g.pickardou Avatar asked Dec 19 '25 21:12

g.pickardou


1 Answers

When you look at the documentation for DbEntityValidationException, you can see the versions it applies to are only: 4.3.1, 5.0.0, 6.2.0.

Now if you look at the docs for SaveChanges (assuming that's what you're calling inside your try block, you can see that it now only throws DpUpdateException and DbUpdateConcurrencyException. So I think what you probably want is the below, since that should catch any exception while saving to the db. I'd think validation would be included in that.

catch (DbUpdateException exception)
{
    // any statements here...
    throw;
}
like image 195
Dan Csharpster Avatar answered Dec 21 '25 13:12

Dan Csharpster



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!