2023/05/20

[EFCore] 執行 Update-Database Migration 時遇到連線資料庫發生憑證錯誤

markdown ### 開發環境 - Visual Studio 2022 v17.6 - ASP.NET Core MVC (.NET 6) - Entity Framework Core 7.x ### 問題 在執行 Update-Database 的 Migration 指令時出現資料庫憑證錯誤的訊息,使得更新資料庫失敗。 錯誤訊息如下: ``` A connection was successfully established with the server, but then an error occurred during the login process. (provider: SSL Provider, error: 0 - 此憑證鏈結是由不受信任的授權單位發出的。) ``` 在本機端開發第一次遇到連線字串需要憑證的問題,過往是連遠端資料庫才會有憑證要求,特此記錄一下。 ### 解決方法 一種是在連線字串後加上 `Encrypt=False;` 不採用加密連線 另一種方式是主動信任 SQL Server 的憑證 `TrustServerCertificate=True;` 在本機開發階段連線不加密或是主動信任憑證是還好,在生產主機上連線就依正常加密及正確的憑證吧! ### 參考資料 - [使用 Microsoft.Data.SqlClient 連線資料庫發生憑證錯誤](https://blog.poychang.net/the-certificate-chain-was-issued-by-an-authority-that-is-not-trusted/) - [MS Docs - Release notes for Microsoft.Data.SqlClient 4.0](https://docs.microsoft.com/en-us/sql/connect/ado-net/introduction-microsoft-data-sqlclient-namespace?view=sql-server-ver15#breaking-changes-in-40?WT.mc_id=DT-MVP-5002629)