Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HasDbFunction can't map void type functions (aka procedures) #33583

Closed
lmtapia opened this issue Apr 21, 2024 · 1 comment
Closed

HasDbFunction can't map void type functions (aka procedures) #33583

lmtapia opened this issue Apr 21, 2024 · 1 comment
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported

Comments

@lmtapia
Copy link

lmtapia commented Apr 21, 2024

I tried mapping user defined functions to methods when an ArgumentExeption was thrown. My functions return explicitly the void type. Also I didn't found any example with the void type.

Previously I had them as Npgsql commands. I'll be using EF SQL querys for now.

Maybe I'm overcomplicating this. I wish to know if this was intended to work or there is another sane way to do it.

User defined function (postgres):

CREATE FUNCTION myschema.updatedeviceid(codcharacter integer, newdeviceid text) RETURNS void
    LANGUAGE plpgsql
    AS $$
    ...
    $$;
CREATE FUNCTION myschema.updatelocation(deviceid text, latitude double precision, longitude double precision) RETURNS void
    LANGUAGE plpgsql
    AS $$  
    ...
    $$;

Code:

using Microsoft.EntityFrameworkCore;

namespace api.Data
{
    public partial class PostgresContext : DbContext
    {
         partial void OnModelCreatingPartial(ModelBuilder modelBuilder) 
         { 
            modelBuilder.HasDbFunction(typeof(PostgresContext).GetMethod(nameof(
                UpdateDeviceId), new[] { typeof(int) , typeof(string) }))
                .HasName("updateDeviceID");
            modelBuilder.HasDbFunction(typeof(PostgresContext).GetMethod(nameof(
                UpdateLocation), new[] { typeof(string), typeof(double), typeof(double) }))
                .HasName("updateLocation");
        }
        public static void UpdateDeviceId(int codCharacter, string deviceId) => throw new NotImplementedException();

        public static void UpdateLocation(string deviceId, double latitude, double longitude) => throw new NotImplementedException();
    }
}

Stack trace:

System.ArgumentException
  HResult=0x80070057
  Message=The DbFunction 'UpdateDeviceId' has an invalid return type 'void'. Ensure that the return type can be mapped by the current provider.
  Source=Microsoft.EntityFrameworkCore.Relational
  StackTrace:
   at Microsoft.EntityFrameworkCore.Metadata.Internal.DbFunction..ctor(String name, Type returnType, IEnumerable`1 parameters, IMutableModel model, ConfigurationSource configurationSource)
   at Microsoft.EntityFrameworkCore.Metadata.Internal.DbFunction..ctor(MethodInfo methodInfo, IMutableModel model, ConfigurationSource configurationSource)
   at Microsoft.EntityFrameworkCore.Metadata.Internal.DbFunction.AddDbFunction(IMutableModel model, MethodInfo methodInfo, ConfigurationSource configurationSource)
   at Microsoft.EntityFrameworkCore.RelationalModelExtensions.AddDbFunction(IMutableModel model, MethodInfo methodInfo)
   at Microsoft.EntityFrameworkCore.RelationalModelBuilderExtensions.HasDbFunction(ModelBuilder modelBuilder, MethodInfo methodInfo)
   at catchflydbapi.Data.PostgresContext.OnModelCreatingPartial(ModelBuilder modelBuilder) in E:\Two\projects\catchfly-dbapi\Data\PostgresContext.cs:line 8
   at catchflydbapi.Data.PostgresContext.OnModelCreating(ModelBuilder modelBuilder) in E:\Two\projects\catchfly-dbapi\Autogenerated\Data\PostgresContext.cs:line 83
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelCustomizer.Customize(ModelBuilder modelBuilder, DbContext context)
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder, ModelDependencies modelDependencies)
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.GetModel(DbContext context, ModelCreationDependencies modelCreationDependencies, Boolean designTime)
   at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel(Boolean designTime)
   at Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model()
   at Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkServicesBuilder.<>c.<TryAddCoreServices>b__8_4(IServiceProvider p)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine.<>c__DisplayClass2_0.<RealizeService>b__0(ServiceProviderEngineScope scope)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
   at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies()
   at Microsoft.EntityFrameworkCore.DbContext.get_ContextServices()
   at Microsoft.EntityFrameworkCore.DbContext.get_Model()
   at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.get_EntityType()
   at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.CheckState()
   at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.get_EntityQueryable()
   at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.System.Collections.Generic.IAsyncEnumerable<TEntity>.GetAsyncEnumerator(CancellationToken cancellationToken)
   at System.Runtime.CompilerServices.ConfiguredCancelableAsyncEnumerable`1.GetAsyncEnumerator()
   at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.<ToListAsync>d__67`1.MoveNext()

Include provider and version information

EF Core version: 8.0.4
Database provider: Npgsql.EntityFrameworkCore.PostgreSQL (Version 8.0.2)
Target framework: .NET 8.0
Operating system: Windows 10
IDE: Visual Studio 2022 17.9.6

@AndriySvyryd
Copy link
Member

We support sproc mapping for these scenarios, though you might need #28439 to be able to map some of them

@AndriySvyryd AndriySvyryd closed this as not planned Won't fix, can't repro, duplicate, stale Apr 26, 2024
@AndriySvyryd AndriySvyryd added the closed-no-further-action The issue is closed and no further action is planned. label Apr 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported
Projects
None yet
Development

No branches or pull requests

2 participants