Search code examples
entity-frameworkstored-functions

Unable to generate function import return type of the store function


I have the following simple saved as a scalar function in my SQLEXPRESS (generated via Visual Studio) and running it via VS as a SQLQuery1.sql works just fine.

CREATE FUNCTION [dbo].[AveragePurchase]
(
@CustomerID int
)
RETURNS DECIMAL(8,3)
AS
BEGIN
DECLARE @Average DECIMAL(8,3)
SELECT @Average = avg(PurchaseAmount)
FROM Purchases1
WHERE CustomersCustomerID = @CustomerID;
RETURN @Average
END

I them go to update the model from the database and run the wizard having checked the function to tell the VS to incorporate it into the model. I seem to be running into the following warning:

Warning 1 Error 6046: Unable to generate function import return type of the store function 'AveragePurchase'. The store function will be ignored and the function import will not be generated.

I must be overlooking something quite obvious but can't seem to locate how I can correct what it is complaining about. any help appreciated. thanks!


Solution

  • Running into same issue and found this answer from microsoft.

    For the scalar UDF, unfortunately, it is not supported by default even in Entity Framework 6 or 6.1. One way to use it in Entity Framework is to create a stored procedure using your scalar UDF and call the stored procedure in your project.

    source: microsoft msdn