EF Core Exceptions Getting Started

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

EntityFrameworkCore.Exceptions.SqlServer is a NuGet library that handles database errors easily when working with Entity Framework Core.

  • When using Entity Framework Core for data access all database exceptions are wrapped in DbUpdateException.
  • If you need to find whether a unique constraint caused the exception, the value being too long or the value missing for a required column, you need to dig into the concrete DbException subclass instance and check the error code to determine the exact cause.
  • You can catch specific exceptions such as UniqueConstraintException, CannotInsertNullException, MaxLengthExceededException, NumericOverflowException or ReferenceConstraintException instead of generic DbUpdateException.

Installation

You can easily install it from the Package Manager Console window by running the following command.

PM> Install-Package EntityFrameworkCore.Exceptions.SqlServer

Environment Setup

To start using the EntityFrameworkCore.Exceptions.SqlServer in your application, you will need to install the EntityFrameworkCore.Exceptions.SqlServer NuGet package.

Let's open the Visual Studio and create a new project.

image

Select the Create a new project option.

image

Choose C# as language, Windows as a platform, and Console as the project type. In the template pane, select Console Application and click the Next button.

image

Enter the project name, you can change the location and solution name, but we will leave it and click on the Next button.

image

On the Additional Information dialog, select the target framework and then click on the Create button.

image

You can see a new console application project is created. Now, to install an EntityFrameworkCore.Exceptions.SqlServer, right-click on the project in Solution Explorer, and select Manage NuGet Packages...

image

Select the Browse tab and search for EntityFrameworkCore.Exceptions.SqlServer and install the latest version by pressing the Install button.

image

Once EntityFrameworkCore.Exceptions.SqlServer has been successfully installed. Let's add the database provider that you want to target. We will use SQL Server, and the provider package is Microsoft.EntityFrameworkCore.SqlServer. We can easily install that NuGet package by executing the following command in Package Manager Console.

PM> Install-Package Microsoft.EntityFrameworkCore.SqlServer

You are now ready to start your application.



Got any EF Core Exceptions Question?