如何传递参数到DbContext.Database.ExecuteSqlCommand方法? [英] How to pass parameters to the DbContext.Database.ExecuteSqlCommand method?
问题描述
var firstName =John;
var id = 12;
var sql = @更新[用户] SET FirstName = @FirstName WHERE Id = @Id;
ctx.Database.ExecuteSqlCommand(sql,firstName,id);
ExecuteSqlCommand方法不允许您传入命名参数,如ADO.Net和< a href =http://msdn.microsoft.com/en-us/library/system.data.entity.database.executesqlcommand%28v=VS.103%29.aspx =noreferrer>此方法的文档
如何正确指定参数?
证明这是有效的。
var firstName =John;
var id = 12;
var sql = @Update [User] SET FirstName = {0} WHERE Id = {1};
ctx.Database.ExecuteSqlCommand(sql,firstName,id);
Let's just suppose I have a valid need for directly executing a sql command in Entity Framework. I am having trouble figuring out how to use parameters in my sql statement. The following example (not my real example) doesn't work.
var firstName = "John";
var id = 12;
var sql = @"Update [User] SET FirstName = @FirstName WHERE Id = @Id";
ctx.Database.ExecuteSqlCommand(sql, firstName, id);
The ExecuteSqlCommand method doesn't allow you to pass in named parameters like in ADO.Net and the documentation for this method doesn't give any examples on how to execute a parameterized query.
How do I specify the parameters correctly?
Turns out that this works.
var firstName = "John";
var id = 12;
var sql = @"Update [User] SET FirstName = {0} WHERE Id = {1}";
ctx.Database.ExecuteSqlCommand(sql, firstName, id);
这篇关于如何传递参数到DbContext.Database.ExecuteSqlCommand方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!