DbParameterCollection does not contain a definition for AddWithValue

asp.net-core c# entity-framework-core

Question

Getting above error on the last line of the following code. I'm using EF Core 1.1. Trying to follow this suggestion.

using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient; //Visual Studio had greyed out this line suggesting this as unnecessary.

var conn = _context.Database.GetDbConnection();

var cmd = conn.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "MySproc";
cmd.Parameters.AddWithValue("@MyParameter", 42);
1
1
5/23/2017 12:26:07 PM

Accepted Answer

Add reference for System.Data.SqlClient and cast your DbConnection instance to SqlConnection.

3
5/12/2017 9:57:29 PM

Popular Answer

Here's the equivalent code using just the System.Data.Common APIs. (It can be used with any ADO.NET provider.)

var parameter = cmd.CreateParameter();
parameter.ParameterName = "@MyParameter";
parameter.Value = 42;

cmd.Parameters.Add(parameter);


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow