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

By Rijwan Ansari

Research and Technology Lead | Software Architect | Full Stack .NET Expert | Tech Blogger | Community Speaker | Trainer | YouTuber. Follow me @ https://rijsat.com Md Rijwan Ansari is a high performing and technology consultant with 10 plus years of Software Development and Business Applications implementation using .NET Technologies, SharePoint, Power Platform, Data, AI, Azure and cognitive services. He is also a Microsoft Certified Trainer, C# Corner MVP, Microsoft Certified Data Analyst Associate, Microsoft Certified Azure Data Scientist Associate, CSM, CSPO, MCTS, MCP, with 15+ Microsoft Certifications. He is a research and technology lead in Tech One Global as well as leading Facebook community Cloud Experts Group and SharePoint User Group Nepal. He is a active contributor and speaker in c-sharpcorner.com community, C# Corner MVP and his rank at 20 among 3+ millions members. Additionally, he is knee to learn new technologies, write articles, love to contribute to the open-source community. Visit his blog RIJSAT.COM for extensive articles, courses, news, videos and issues resolution specially for developer and data engineer.

Leave a Reply

Your email address will not be published.