Recently, I was getting an error while initializing the database with code-first approach with update-migration command in my asp.net core solution.

Error

A connection was successfully established with the server, but then an error occurred during the login process. (provider: SSL Provider, error: 0 – The certificate chain was issued by an authority that is not trusted.)

My setup

I was using a local MS SQL Database and default window authentication. My connection sting was correct, and I was able to login into the database using SQL Server Management Studio without any issue.

My solution was asp.net core with .NET framework 7 and entity framework Code-First approach.

I was able to run a migration command (Add-Migration), however, I was getting the above error while updating the database with update-database command.

The exact error is given below.

Error

A connection was successfully established with the server, but then an error occurred during the login process. (provider: SSL Provider, error: 0 – The certificate chain was issued by an authority that is not trusted.)

Solution

Let’s understand the prompt message in more details.

The issue started from release of Microsoft.Data.SqlClient 4.0, specifically because of a new change that enable the encryption by default true.

https://learn.microsoft.com/en-us/sql/connect/ado-net/introduction-microsoft-data-sqlclient-namespace?view=sql-server-ver15#encrypt-default-value-set-to-true

The default value of the Encrypt connection setting has been changed from false to true. With the growing use of cloud databases and the need to ensure those connections are secure, it’s time for this backwards-compatibility-breaking change.

Therefore, we can fix this issue by changing the connection string as example shown below:

"ConnectionStrings": {
    "AppDatabase": "Data Source=rijwan7475001;Initial Catalog=mydotnet7;Integrated Security=True;trusted_connection=true;encrypt=false;"
  }

We need to add additional parameters trusted_connection=true;encrypt=false; in the connection string.

Cheers