DotNetCore系列:解決"The SqlParameterCollection only accepts

近段時間對公司的.NET Core項目升級為3.1,因為我們項目中使用的ORM框架是EF Core,所以順帶就把EF Core升級為3.1。

升級後對項目做了基本的調試和運行,發現在執行存儲過程的時候,居然神奇的報錯了。因為我並沒有改動任何代碼,所以非常疑惑為什麼升級之後,原來好端端的代碼會出錯了!!!

<code>ystem.InvalidCastException: The SqlParameterCollection only accepts non-null SqlParameter type objects, not SqlParameter objects.
at Microsoft.Data.SqlClient.SqlParameterCollection.ValidateType(Object value)
at Microsoft.Data.SqlClient.SqlParameterCollection.Add(Object value)
at Microsoft.EntityFrameworkCore.Storage.Internal.RawRelationalParameter.AddDbParameter(DbCommand command, Object value)
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.CreateCommand(RelationalCommandParameterObject parameterObject, Guid commandId, DbCommandMethod commandMethod)
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteNonQueryAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.ExecuteSqlRawAsync(DatabaseFacade databaseFacade, String sql, IEnumerable`1 parameters, CancellationToken cancellationToken)
/<code>

經過仔細查閱微軟關於EF Core升級的文檔,發現是因為EF Core3.0之後,有一個依賴包變更了,由原來的

<code>> using System.Data.SqlClient/<code>

變更為

<code>> using Microsoft.Data.SqlClient/<code>

通過更改新的引用依賴包後,執行存儲過程時就恢復正常了。

詳細的一些發佈說明大家可以查看微軟的官方文檔:

<code>> [EF Core 3.0 中包含的中斷性變更](https://docs.microsoft.com/zh-cn/ef/core/what-is-new/ef-core-3.0/breaking-changes)
> /<code>


分享到:


相關文章: