如何在Entity Framework Core(Oracle)中使用自定义函数?

plupiseo  于 2023-06-22  发布在  Oracle
关注(0)|答案(1)|浏览(130)

我刚开始在. NET Core 3.1项目中使用Entity Framework Core与Oracle数据库,并且在尝试调用用户定义函数时不断遇到问题。
数据库中的函数如下所示:

FUNCTION fnc_hourly_data(
    p_quality   IN t_quality_configurations.quality%TYPE DEFAULT NULL,
    p_date       IN DATE DEFAULT SYSDATE)
    RETURN hourly_data_arr_type IS
    v_result   hourly_data_arr_type := hourly_data_arr_type ();

我尝试通过this教程将一个方法Map到这个函数,所以我将以下代码添加到ModelContextOnModelCreating函数:

modelBuilder.HasDbFunction(typeof(ModelContext).GetMethod(nameof(FncHourlyData), new[] { typeof(IEnumerable<HourlyData>) })).HasName("fnc_hourly_data");

我还按照教程中的描述定义了这个函数:

public IEnumerable<HourlyData> FncHourlyData(string quality, DateTime date) => throw new NotSupportedException();

当我现在执行应用程序时,我得到以下错误:
System.ArgumentNullException
HResult = 0x80004003
消息=值不能为空。
源= Microsoft. EntityFrameworkCore. Relational
StackTrace:
at Microsoft. EntityFrameworkCore. Utilities. Check. NotNull [T](T value,String parameterName)
位于Microsoft. EntityFrameworkCore. RelationalModelBuilderExtensions. HasDbFunction(模型构建器modelBuilder,MethodInfo methodInfo)
at ORMTest. Model. HourlyDataModel. ModelContext. OnModelCreating(ModelBuilder modelBuilder)in C:\Projects...\ModelContext. cs:line 49
位于Microsoft. EntityFrameworkCore. Infrastructure. ModelCustomizer. Customize(模型构建器modelBuilder,DbContext上下文)
在Microsoft. EntityFrameworkCore. Infrastructure. ModelSource. CreateModel(DbContext上下文,IConventionSetBuilder conventionSetBuilder)
在Microsoft. EntityFrameworkCore. Infrastructure. ModelSource. GetModel(DbContext上下文,IConventionSetBuilder conventionSetBuilder)
位于Microsoft. EntityFrameworkCore. Internal. DbContextServices. CreateModel()
位于Microsoft. EntityFrameworkCore. Internal. DbContextServices. get_Model()
位于Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkServicesBuilder.<> c.b__7_3(IServiceProvider p)
at Microsoft. Extensions. DependencyInjection. ServiceLookup. CallSiteRuntimeResolver. VisitFactory(FactoryCallSite factoryCallSite,RuntimeResolverContext context)
在微软。扩展。DependencyInjection.服务查找。Microsoft的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)。扩展。DependencyInjection.服务查找。CallSiteRuntimeResolver。Microsoft中的VisitConstructor(ConstructorCallSite constructorCallSite,RuntimeResolverContext context)。扩展。DependencyInjection.服务查找。Microsoft的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)。扩展。DependencyInjection.服务查找。CallSiteRuntimeResolver。在Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine中解析(ServiceCallSite callSite,ServiceProviderEngineScope范围)。<> c__DisplayClass1_0.b__0(ServiceProviderEngineScope范围),位于Microsoft。扩展。DependencyInjection.服务查找。ServiceProviderEngine。Microsoft中的GetService(Type serviceType,ServiceProviderEngineScope serviceProviderEngineScope)。扩展。DependencyInjection.服务查找。ServiceProviderEngineScope。Microsoft的GetService(Type serviceType)。扩展。DependencyInjection. ServiceProviderServiceExtensions。Microsoft的GetRequiredService(IServiceProvider提供程序,类型serviceType)。扩展。DependencyInjection. ServiceProviderServiceExtensions。Microsoft的GetRequiredService [T](IServiceProvider提供程序)。EntityFrameworkCore. DbContext。Microsoft的get_DbContextDependencies()。EntityFrameworkCore. DbContext。Microsoft的get_InternalServiceProvider()。EntityFrameworkCore. DbContext。Microsoft的get_DbContextDependencies()。EntityFrameworkCore. DbContext。get_Model()在Microsoft。EntityFrameworkCore.内部。Microsoft的InternalDbSet 1.get_EntityType() at Microsoft.EntityFrameworkCore.Internal.InternalDbSet 1.CheckState()。EntityFrameworkCore.内部。InternalDbSet 1.get_EntityQueryable() at Microsoft.EntityFrameworkCore.Internal.InternalDbSet 1.System.Linq.IQueryable.get_Provider()at System. Linq.可查询。其中[TSource](IQueryable 1 source, Expression 1 predicate )位于ORMTest。程序。C中的Main(String [] args):\项目...程序。cs:第14行
根据错误,参数'methodInfo'在这里是null,因此导致这个异常被抛出,但我不明白这里有什么问题。如何在项目中正确定义此函数以便调用它?是否有其他(更简单)的方法来调用该函数?

ltskdhd1

ltskdhd11#

GetMethod的第二个参数是“System.Type对象的数组,表示要获取的方法的参数的数量、顺序和类型”。

modelBuilder
  .HasDbFunction(
    typeof(ModelContext)
      .GetMethod(
        nameof(FncHourlyData),
        new[] { typeof(string), typeof(DateTime) }
      )
  )
  .HasName("fnc_hourly_data");

相关问题