Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ExecutesSqlRaw throwing weird error about "SqlParameterCollection only accepts non-null SqlParameter type objects" #18218

Closed
BobbyCannon opened this issue Oct 4, 2019 · 6 comments

Comments

@BobbyCannon
Copy link

I'm trying to execute a raw sql statement with a sql parameter that is not null but I'm getting an odd error.

"UPDATE [a] SET [a].[IsDeleted] = @param_0 FROM [dbo].[Addresses] AS [a] WHERE [a].[IsDeleted] = 0"

"SqlParameterCollection only accepts non-null SqlParameter type objects"

image

image

Steps to reproduce

Further technical details

EF Core version: 3
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET Core 3.0
Operating system: Windows 10 1903 - 18362.356
IDE: Visual Studio 2019 16.3.2

@usmalik78
Copy link

Just upgraded to .Net Core 3.0 and EF Core 3.0 and ran into this exact issue with ExecuteSqlRaw/FromSqlRaw. A temporary workaround is to use ExecuteSqlInterpolated/FromSqlInterpolated and provide it individual strings instead of SqlParameter or SqlParameterCollection. This workaround will still create parameterized statements so it is still safe. However, if you end up executing a stored procedure with either of the "Interpolated" methods, make sure that your stored procedure parameter order is the exact same as your statement parameter order (parameter names are irrelevant). For this reason, we are going to revert to EF Core 2.2 until this issue is fixed because we primarily invoke stored procedures and don't want to take this chance.

@snerks
Copy link

snerks commented Oct 4, 2019

Have you tried:

Replace:
using System.Data.SqlClient;
with
using Microsoft.Data.SqlClient;

@BobbyCannon
Copy link
Author

@snerks, how in the world did you know that? I just swapped nuget packages and change the using statement and everything worked.

@roji
Copy link
Member

roji commented Oct 4, 2019

EF Core 3.0 changed its dependency from System.Data.SqlClient to the newly-released Microsoft.Data.SqlClient. This doesn't affect most scenarios, but if you're passing SqlParameter instances to raw SQL methods, you indeed need to make sure you're passing the right type.

Opened dotnet/EntityFramework.Docs#1815 to document.

@roji roji added closed-no-further-action The issue is closed and no further action is planned. and removed type-bug labels Oct 4, 2019
@snerks
Copy link

snerks commented Oct 6, 2019

@jamesgurung solved it:

#16812 (comment)

@roji roji added closed-duplicate and removed closed-no-further-action The issue is closed and no further action is planned. labels Oct 7, 2019
@roji
Copy link
Member

roji commented Oct 7, 2019

Duplicate of #16812

@roji roji marked this as a duplicate of #16812 Oct 7, 2019
@roji roji closed this as completed Oct 7, 2019
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants