Question

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?

Was it helpful?

Solution

Like that:

CreateStoredProcedure("SumInvoiceTransactions", c => new { InvoiceId = c.Int() },
"RETURN (SELECT SUM(Amount) FROM Transactions WHERE InvoiceId=@InvoiceId)");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top