EFCorePowerTools icon indicating copy to clipboard operation
EFCorePowerTools copied to clipboard

Table valued parameter (TVP) throwing errors: `System.InvalidOperationException: The parameter for the DbFunction has an invalid type 'DataTable'. Ensure the parameter type can be mapped by the current provider.`

Open Aspyryan opened this issue 1 year ago • 2 comments

As described above, when trying to run a table valued function with a table valued parameter through ef core I get the error. This is the code:

public async Task<PagedEnumerable<vwCupJettyPlanningFunctionResult>> Search(ApplicationEnum application, RequestFilter criteria)
        {
            await using var ctx = await _ctxFactory.CreateDbContextAsync();
            var customerCodes = new DataTable();
            customerCodes.Columns.Add("CustomerCode", typeof(int));

            foreach (var customerCode in CustomerCodeResolverService.Customers!.Select(c => c.CustomerCode))
            {
                customerCodes.Rows.Add(customerCode);
            }
            
            var q = await ctx.vwCupJettyPlanningFunction(customerCodes).SearchWithFilter();
                
            return new PagedEnumerable<vwCupJettyPlanningFunctionResult> { Total = q.count, Data = await q.query.ToListAsync() };
        }

error occurs on ctx.vwCupJettyPlanningFunction(customerCodes)

SQL: (rep_CustomerCodeListType.sql)

CREATE TYPE rep.CustomerCodeListType
AS TABLE
(
    CustomerCode INT
);
GO

(rep_Function SIMPLIFIED)

CREATE or ALTER FUNCTION [rep].[vwCupJettyPlanningFunction](
    @CustomerCodes AS rep.CustomerCodeListType READONLY
)
    RETURNS TABLE AS RETURN
        SELECT *
        FROM Orders vo
                 LEFT JOIN rep.vwOrderOverview oo on vo.OrderId = oo.OrderId and oo.CustomerCode in (SELECT CustomerCode from @CustomerCodes)

Error: image

Aspyryan avatar Aug 29 '24 09:08 Aspyryan

Not supported by EF Core as far as I can tell.

ErikEJ avatar Aug 29 '24 14:08 ErikEJ

Ah okay no worries, fixed it with string with comma delimiter

Aspyryan avatar Aug 29 '24 14:08 Aspyryan