I'm trying to use DbMigration.CreateStoredProcedure to create a function that takes an integer InvoiceId and returns a decimal. However, when I apply the migration, I get this error:

Must declare the scalar variable "@InvoiceId".

This is the EF migration code:

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

This is the equivalent SQL code:

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

How do I declare the parameter?

有帮助吗?

解决方案

Like that:

CreateStoredProcedure("SumInvoiceTransactions", c => new { InvoiceId = c.Int() },
"RETURN (SELECT SUM(Amount) FROM Transactions WHERE InvoiceId=@InvoiceId)");
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top