简体   繁体   English

实体框架ExecuteSqlCommand缺少输入参数

[英]Entity Framework ExecuteSqlCommand Missing Input Parameter

I'm having an issue with the ExecuteSqlCommand() method in Entity Framework (EF). 我在Entity Framework(EF)中的ExecuteSqlCommand()方法遇到问题。 I have the following SQL Server stored procedure: 我有以下SQL Server存储过程:

EXEC SomeSchema.SomeTableInsert
@Id = 1, 
@SomeData = 'This is some random data', 
@UserId = 500

and it works without any issues. 而且没有任何问题。 When I try to use EF's ExecuteSqlCommand() method, I get the following error: 当我尝试使用EF的ExecuteSqlCommand()方法时,出现以下错误:

Procedure or function 'SomeTableInsert' expects parameter '@UserId', which was not supplied.

Here is the C# code: 这是C#代码:

var sql = "SomeSchema.SomeTableInsert @Id, @SomeData, @UserId";
var sqlParameters = new List<SqlParameter> 
{ 
    new SqlParameter("@Id", 1),
    new SqlParameter("@SomeData", "This is some random data"), 
    new SqlParameter("@UserId", 500)
};

dbContext.Database.ExecuteSqlCommand(sql, sqlParameters.ToArray<object>());

I don't understand why I'm getting this error. 我不明白为什么会收到此错误。 The UserId parameter is not missing and it has a valid value. UserId参数不丢失,并且具有有效值。 Any ideas? 有任何想法吗?

Update: 更新:

Here is the SQL that is run on SQL Server: 这是在SQL Server上运行的SQL:

exec sp_executesql 
    N'SomeSchema.SomeTableInsert @Id, @SomeData, @UserId',
    N'@Id int,@SomeData nvarchar(5),@UserId int',
    @Id=1,
    @SomeData='This is some random data',
    @UserId=500
var sql = "SomeSchema.SomeTableInsert @Id, @SomeData, @UserId";
var sqlParameters = new List<SqlParameter> 
{ 
    new SqlParameter("Id", 1),
    new SqlParameter("SomeData", "This is some random data"), 
    new SqlParameter("UserId", 500)
};

dbContext.Database.ExecuteSqlCommand(sql, sqlParameters.ToArray<object>());

Remove the @ prefix from within the SqlParameter. 从SqlParameter中删除@前缀。

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

相关问题 实体框架ExecuteSqlCommand批量更新 - Entity Framework ExecuteSqlCommand bulk update 为什么存储过程out参数没有被实体框架的ExecuteSqlCommand方法读取? - Why stored procedure out parameter is not getting read by entity framework's ExecuteSqlCommand method? 实体框架:Database.ExecuteSqlCommand方法 - Entity Framework: Database.ExecuteSqlCommand Method System.Not Supported 用于模拟 ExecuteSQLCommand Moq Entity Framework Core 的异常 - System.Not Supported exception for mocking ExecuteSQLCommand Moq Entity Framework Core Entity Framework Core ExecuteSqlCommand 删除 SQLite 不起作用 - Entity Framework Core ExecuteSqlCommand delete with SQLite does not work C# 覆盖实体框架 Database.ExecuteSqlCommand - C# override Entity Framework Database.ExecuteSqlCommand 如何使用 ExecuteSqlCommand 删除实体框架中的记录? - How can I delete records in Entity Framework using ExecuteSqlCommand? Moq 实体框架 ExecuteSQLCommand - Moq Entity Frameworks ExecuteSQLCommand 当命令文本具有DDL和DML指令时,ExecuteSqlCommand在实体框架中失败 - ExecuteSqlCommand fails in Entity Framework when the command text has a DDL and DML instruction 使用实体框架的ExecuteSqlCommand执行的TRUNCATE TABLE的可能返回值是什么? - What are the possible return values of TRUNCATE TABLE executed with Entity Framework's ExecuteSqlCommand?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM