将变量声明为DbMigration.CreateStoredProcedure

时间:2014-05-09 17:27:46

标签: sql-server entity-framework ef-migrations

我尝试使用DbMigration.CreateStoredProcedure创建一个取整数InvoiceId并返回小数的函数。但是,当我应用迁移时,我收到此错误:

  

必须声明标量变量" @ InvoiceId"。

这是EF迁移代码:

CreateStoredProcedure("SumInvoiceTransactions", b => b.Decimal(9, 2),
    "RETURN (SELECT SUM(Amount) FROM Transactions WHERE InvoiceId=@InvoiceId)");

这是等效的SQL代码:

CREATE FUNCTION SumInvoiceTransactions(@InvoiceId int) RETURNS decimal
AS BEGIN
  RETURN (SELECT sum(Amount) FROM Transactions WHERE InvoiceId=@InvoiceId)
END

如何声明参数?

1 个答案:

答案 0 :(得分:2)

就像那样:

CreateStoredProcedure("SumInvoiceTransactions", c => new { InvoiceId = c.Int() },
"RETURN (SELECT SUM(Amount) FROM Transactions WHERE InvoiceId=@InvoiceId)");