簡體   English   中英

DbParameterCollection 不包含 AddWithValue 的定義

[英]DbParameterCollection does not contain a definition for AddWithValue

在以下代碼的最后一行出現上述錯誤。 我正在使用EF Core 1.1 試圖遵循這個建議。

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);

這是僅使用 System.Data.Common API 的等效代碼。 (它可以與任何 ADO.NET 提供程序一起使用。)

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

cmd.Parameters.Add(parameter);

添加對 System.Data.SqlClient 的引用並將您的 DbConnection 實例轉換為 SqlConnection。

在 dotnet core 3+ 中,運行命令被破壞。 要修復它,請將 System.Data.SqlClient 更改為 Microsoft.Data.SqlClient

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM