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

NRE when trying to configure parameter on TVF in the ToFunction call during model configuration #23406

Open
maumar opened this issue Nov 19, 2020 · 2 comments

Comments

@maumar
Copy link
Contributor

maumar commented Nov 19, 2020

udf:

create function [dbo].PostsTaggedWith(@tag varchar(max))
                    returns @posts table
                    (
                        PostId int not null,
                        BlogId int not null,
                        Content nvarchar(max),
                        Rating int not null,
                        Title nvarchar(max)
                    )
                    as
                    begin
                        insert into @posts
                        select p.PostId, p.BlogId, p.Content, p.Rating, p.Title
                        from Posts as p
                        where exists (
                            select 1
                            from PostTag AS pt
                            inner join Tags AS t ON pt.TagId = t.TagId
                            where (p.PostId = pt.PostId) AND (t.TagId = @tag))

                        return
                    end

clr function:

            public IQueryable<Post> PostsTaggedWith(string tag)
                => FromExpression(() => PostsTaggedWith(tag));

model builder call:

                modelBuilder.Entity<Post>().ToFunction("PostsTaggedWith", b => b.HasParameter("tag").HasStoreType("nvarchar(max)"));

exception:

   at Microsoft.EntityFrameworkCore.Internal.MethodInfoExtensions.DisplayName(MethodInfo methodInfo) in D:\git\efcore\src\EFCore.Relational\Extensions\Internal\MethodInfoExtensions.cs:line 25
   at Microsoft.EntityFrameworkCore.Metadata.Builders.Internal.InternalDbFunctionBuilder.HasParameter(String name, ConfigurationSource configurationSource) in D:\git\efcore\src\EFCore.Relational\Metadata\Internal\InternalDbFunctionBuilder.cs:line 244
   at Microsoft.EntityFrameworkCore.Metadata.Builders.DbFunctionBuilderBase.HasParameter(String name) in D:\git\efcore\src\EFCore.Relational\Metadata\Builders\DbFunctionBuilderBase.cs:line 97
   at Microsoft.EntityFrameworkCore.Query.QueryBugsTest.BloggingContext.<>c.<OnModelCreating>b__16_8(TableValuedFunctionBuilder b) in D:\git\efcore\test\EFCore.SqlServer.FunctionalTests\Query\QueryBugsTest.cs:line 9540
   at Microsoft.EntityFrameworkCore.RelationalEntityTypeBuilderExtensions.ToFunction(EntityTypeBuilder entityTypeBuilder, String name, Action`1 configureFunction) in D:\git\efcore\src\EFCore.Relational\Extensions\RelationalEntityTypeBuilderExtensions.cs:line 752
   at Microsoft.EntityFrameworkCore.RelationalEntityTypeBuilderExtensions.ToFunction[TEntity](EntityTypeBuilder`1 entityTypeBuilder, String name, Action`1 configureFunction) in D:\git\efcore\src\EFCore.Relational\Extensions\RelationalEntityTypeBuilderExtensions.cs:line 783
   at Microsoft.EntityFrameworkCore.Query.QueryBugsTest.BloggingContext.OnModelCreating(ModelBuilder modelBuilder) in D:\git\efcore\test\EFCore.SqlServer.FunctionalTests\Query\QueryBugsTest.cs:line 9540
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelCustomizer.Customize(ModelBuilder modelBuilder, DbContext context) in D:\git\efcore\src\EFCore\Infrastructure\ModelCustomizer.cs:line 56
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder, ModelDependencies modelDependencies) in D:\git\efcore\src\EFCore\Infrastructure\ModelSource.cs:line 145
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.GetModel(DbContext context, IConventionSetBuilder conventionSetBuilder, ModelDependencies modelDependencies) in D:\git\efcore\src\EFCore\Infrastructure\ModelSource.cs:line 100
   at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel() in D:\git\efcore\src\EFCore\Internal\DbContextServices.cs:line 85
   at Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model() in D:\git\efcore\src\EFCore\Internal\DbContextServices.cs:line 112
   at Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkServicesBuilder.<>c.<TryAddCoreServices>b__7_3(IServiceProvider p) in D:\git\efcore\src\EFCore\Infrastructure\EntityFrameworkServicesBuilder.cs:line 256
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, RuntimeResolverContext context)
   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 singletonCallSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
   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 singletonCallSite, 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__DisplayClass1_0.<RealizeService>b__0(ServiceProviderEngineScope scope)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
   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() in D:\git\efcore\src\EFCore\DbContext.cs:line 394
   at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider() in D:\git\efcore\src\EFCore\DbContext.cs:line 376
   at Microsoft.EntityFrameworkCore.DbContext.Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<System.IServiceProvider>.get_Instance() in D:\git\efcore\src\EFCore\DbContext.cs:line 1787
   at Microsoft.EntityFrameworkCore.Infrastructure.Internal.InfrastructureExtensions.GetService[TService](IInfrastructure`1 accessor) in D:\git\efcore\src\EFCore\Infrastructure\Internal\InfrastructureExtensions.cs:line 33
   at Microsoft.EntityFrameworkCore.Infrastructure.AccessorExtensions.GetService[TService](IInfrastructure`1 accessor) in D:\git\efcore\src\EFCore\Infrastructure\AccessorExtensions.cs:line 46
   at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.get_Dependencies() in D:\git\efcore\src\EFCore\Infrastructure\DatabaseFacade.cs:line 40
   at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.EnsureDeleted() in D:\git\efcore\src\EFCore\Infrastructure\DatabaseFacade.cs:line 143
@maumar
Copy link
Contributor Author

maumar commented Nov 19, 2020

note:

                modelBuilder.HasDbFunction(typeof(BloggingContext).GetMethod(nameof(PostsTaggedWith), new[] { typeof(string) }))
                    .HasParameter("tag").HasStoreType("nvarchar(max)");

works fine

@smitpatel
Copy link
Contributor

Fixed in e45256e
We could improve exception further by coalescing with function name or pointing out that we don't know method info on it.

@ajcvickers ajcvickers added this to the 6.0.0 milestone Nov 20, 2020
@smitpatel smitpatel self-assigned this Sep 13, 2021
@smitpatel smitpatel removed this from the 6.0.0 milestone Sep 15, 2021
@AndriySvyryd AndriySvyryd added this to the MQ milestone Sep 15, 2021
@smitpatel smitpatel removed their assignment Nov 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants