问题
Could someone help with passing a parameter object into my ExecuteStoreQuery
. I'm executing this from my Entities db and then creating my procedure on the fly is this possible and is my SQL correct. I need to pass a Id
parameter value into my SQL statement
var node = db.ExecuteStoreQuery<Node>(@"
@Id int // not sure about this declaration for parameter
with c as (
select Id, Name, ParentId,[0] as level
from Department d
union all
select d.Id, d.Name, d.ParentId, [level] + 1
from Department d
join c on d.ParentId = c.Id)
select * from c where c.Id = @Id"
"Departments",
System.Data.Objects.MergeOption.NoTracking,
new object{ Id: department.Id} // <--parameter object not working);
回答1:
According to the MSDN documentation, your statement needs to read more like:
var node = db.ExecuteStoreQuery<Node>(@"
with c as (
select Id, Name, ParentId,[0] as level
from Department d
union all
select d.Id, d.Name, d.ParentId, [level] + 1
from Department d
join c on d.ParentId = c.Id)
select * from c where c.Id = @Id"
"Departments",
System.Data.Objects.MergeOption.NoTracking,
new SqlParameter{ ParameterName = "id", Value = department.Id});
来源:https://stackoverflow.com/questions/8191212/executestorequery-passing-parameter-to-sql-statement