ExecuteSqlCommand方法

ExecuteSqlCommand方法

本文介绍了如何传递参数到DbContext.Database.ExecuteSqlCommand方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我们假设我有实际需要在Entity Framework中直接执行一个sql命令。我无法弄清楚如何在我的sql语句中使用参数。以下示例(不是我的实例)不起作用。 var firstName =John; var id = 12; var sql = @更新[用户] SET FirstName = @FirstName WHERE Id = @Id; ctx.Database.ExecuteSqlCommand(sql,firstName,id); ExecuteSqlCommand方法不允许您传入命名参数,如ADO.Net和此方法的文档 如何正确指定参数? 解决方案证明这是有效的。 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方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 11:46