简体   繁体   English

DbParameterCollection 不包含 AddWithValue 的定义

[英]DbParameterCollection does not contain a definition for AddWithValue

Getting above error on the last line of the following code.在以下代码的最后一行出现上述错误。 I'm using EF Core 1.1 .我正在使用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);

Here's the equivalent code using just the System.Data.Common APIs.这是仅使用 System.Data.Common API 的等效代码。 (It can be used with any ADO.NET provider.) (它可以与任何 ADO.NET 提供程序一起使用。)

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

cmd.Parameters.Add(parameter);

添加对 System.Data.SqlClient 的引用并将您的 DbConnection 实例转换为 SqlConnection。

In dotnet core 3+ running command is broken.在 dotnet core 3+ 中,运行命令被破坏。 To fix it, change System.Data.SqlClient to Microsoft.Data.SqlClient要修复它,请将 System.Data.SqlClient 更改为 Microsoft.Data.SqlClient

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM