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

SQLite doesn't work when trimming #27311

Closed
NiubilityOptimus opened this issue Jan 29, 2022 · 8 comments · Fixed by #27910
Closed

SQLite doesn't work when trimming #27311

NiubilityOptimus opened this issue Jan 29, 2022 · 8 comments · Fixed by #27910
Assignees
Labels
area-sqlite closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported Servicing-approved type-bug
Milestone

Comments

@NiubilityOptimus
Copy link

Description

当我使用asp.net webapi在构建一个服务时,使用了efcore,数据库使用的是sqlite,但我在debug模式下进行调试开发的时候api接口可以正常调用,但是当我release之后(单文件single file),接口就无法调用,无论我通过visual studio进行publish还是通过dotnet publish -c Release -r win-x64 --self-contained -p:PublishSingleFile=True -p:IncludeNativeLibrariesForSelfExtract=true -p:PublishTrimmed=True -p:ReadyToRun=True进行打包发布,接口调用都无法执行,根据错误日志System.InvalidOperationException: Could not find method 'AddYears' on type 'System.DateOnly'中的提示,我没有找到我的应用程序中sqlite中有datetime或者dateonly类型的实体model,我的dbcontext中也没有对应datetime的字段或者dateonly的字段没所以我不清楚这个问题是从何而来

[11:15:01 INF] Now listening on: http://[::]:11510
[11:15:01 INF] Application started. Press Ctrl+C to shut down.
[11:15:01 INF] Hosting environment: Production
[11:15:01 INF] Content root path: E:\mycesdl\cesdl\src\samples\Api.Server\bin\Release\net6.0\win-x64\publish\
[11:15:05 INF] Request starting HTTP/1.1 GET http://localhost:11510/swagger/index.html - -
[11:15:05 INF] Request finished HTTP/1.1 GET http://localhost:11510/swagger/index.html - - - 200 - text/html;charset=utf-8 462.3572ms
[11:15:06 INF] Request starting HTTP/1.1 GET http://localhost:11510/swagger/v1/swagger.json - -
[11:15:06 INF] Request finished HTTP/1.1 GET http://localhost:11510/swagger/v1/swagger.json - - - 200 - application/json;charset=utf-8 172.3824ms
[11:15:15 INF] Request starting HTTP/1.1 GET http://localhost:11510/api/protocols?PageIndex=1 - -
[11:15:16 ERR] Connection id "0HMF2QM5U8A51", Request id "0HMF2QM5U8A51:00000004": An unhandled exception was thrown by the application.
System.InvalidOperationException: Could not find method 'AddYears' on type 'System.DateOnly'
   at System.SharedTypeExtensions.GetRequiredRuntimeMethod(Type type, String name, Type[] parameters)
   at Microsoft.EntityFrameworkCore.Sqlite.Query.Internal.SqliteDateTimeAddTranslator..ctor(ISqlExpressionFactory sqlExpressionFactory)
   at Microsoft.EntityFrameworkCore.Sqlite.Query.Internal.SqliteMethodCallTranslatorProvider..ctor(RelationalMethodCallTranslatorProviderDependencies dependencies)
   at System.RuntimeMethodHandle.InvokeMethod(Object , Span`1& , Signature , Boolean , Boolean )
   at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags , Binder , Object[] , CultureInfo )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite , RuntimeResolverContext )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite , RuntimeResolverContext , ServiceProviderEngineScope , RuntimeResolverLock )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite , RuntimeResolverContext )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite , TArgument )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite , RuntimeResolverContext )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite , RuntimeResolverContext , ServiceProviderEngineScope , RuntimeResolverLock )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite , RuntimeResolverContext )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite , TArgument )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite , RuntimeResolverContext )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite , RuntimeResolverContext , ServiceProviderEngineScope , RuntimeResolverLock )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite , RuntimeResolverContext )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite , TArgument )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite , RuntimeResolverContext )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite , RuntimeResolverContext , ServiceProviderEngineScope , RuntimeResolverLock )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite , RuntimeResolverContext )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite , TArgument )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite , RuntimeResolverContext )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite , RuntimeResolverContext , ServiceProviderEngineScope , RuntimeResolverLock )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite , RuntimeResolverContext )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite , TArgument )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite , RuntimeResolverContext )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite , RuntimeResolverContext , ServiceProviderEngineScope , RuntimeResolverLock )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite , RuntimeResolverContext )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite , TArgument )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite , RuntimeResolverContext )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite , RuntimeResolverContext , ServiceProviderEngineScope , RuntimeResolverLock )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite , RuntimeResolverContext )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite , TArgument )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite , RuntimeResolverContext )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite , RuntimeResolverContext , ServiceProviderEngineScope , RuntimeResolverLock )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite , RuntimeResolverContext )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite , TArgument )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite , RuntimeResolverContext )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite , RuntimeResolverContext , ServiceProviderEngineScope , RuntimeResolverLock )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite , RuntimeResolverContext )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite , TArgument )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite , RuntimeResolverContext )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite , RuntimeResolverContext , ServiceProviderEngineScope , RuntimeResolverLock )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite , RuntimeResolverContext )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite , TArgument )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite , RuntimeResolverContext )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite , RuntimeResolverContext , ServiceProviderEngineScope , RuntimeResolverLock )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite , RuntimeResolverContext )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite , TArgument )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite , RuntimeResolverContext )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite , RuntimeResolverContext , ServiceProviderEngineScope , RuntimeResolverLock )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite , RuntimeResolverContext )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite , TArgument )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine.<>c__DisplayClass2_0.<RealizeService>b__0(ServiceProviderEngineScope )
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type , ServiceProviderEngineScope )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type )
   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_InternalServiceProvider()
   at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies()
   at Microsoft.EntityFrameworkCore.DbContext.Set[TEntity]()
   at Cesdl.RepositoryBase`2.Filter(Expression`1 exp) in E:\mycesdl\cesdl\src\cesdl\RepositoryBase.cs:line 139
   at Cesdl.RepositoryBase`2.Find(Expression`1 exp) in E:\mycesdl\cesdl\src\cesdl\RepositoryBase.cs:line 24
   at Cesdl.Middleware.CacheMiddleware.<Invoke>b__4_0(ICacheEntry cacheEntry) in E:\mycesdl\cesdl\src\cesdl\Middleware\CacheMiddleware.cs:line 34
   at Microsoft.Extensions.Caching.Memory.CacheExtensions.GetOrCreateAsync[TItem](IMemoryCache , Object , Func`2 )
   at Cesdl.Middleware.CacheMiddleware.Invoke(HttpContext context) in E:\mycesdl\cesdl\src\cesdl\Middleware\CacheMiddleware.cs:line 31
   at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
   at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)
[11:15:16 INF] Request finished HTTP/1.1 GET http://localhost:11510/api/protocols?PageIndex=1 - - - 500 0 - 910.3468ms

Configuration

.net6.0.1
win10 1836
x64
blazorwasm in nginx Error 403

Regression?

@davidfowl davidfowl transferred this issue from dotnet/aspnetcore Jan 29, 2022
@roji
Copy link
Member

roji commented Jan 29, 2022

Duplicate of #26288

@roji roji marked this as a duplicate of #26288 Jan 29, 2022
@NiubilityOptimus

This comment was marked as spam.

@roji
Copy link
Member

roji commented Jan 29, 2022

EF Core 6.0 isn't yet compatible for trimming friendliness, so errors such as the above may occur. Trimming friendliness is a goal for 7.0, in the meantime you should be able to work around this by specifying DateOnly.AddYears (and other missing APIs) in a root descriptor file.

@ajcvickers we could patch 6.0 with a low-risk hack to unblock this, much like we did in #27098 for Math, though we can also wait for more feedback.

@ajcvickers
Copy link
Member

Note from triage: workaround is sufficient for now. We may patch if we get more reports.

@roji roji changed the title I am not sure the problem comes from efcore or .net sdk SQLite doesn't work when trimming Mar 1, 2022
@roji
Copy link
Member

roji commented Mar 1, 2022

Reopening to rediscuss patching this as per #27474 (comment) (this would be very low-risk, similar to #27098).

@roji roji reopened this Mar 1, 2022
@NiubilityOptimus

This comment was marked as spam.

@ajcvickers
Copy link
Member

Note for triage: bring to Tactics to patch for MAUI experience.

roji added a commit to roji/efcore that referenced this issue Apr 28, 2022
Allows SQLite to be used when trimming.

Fixes dotnet#27311
@roji roji added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Apr 28, 2022
@roji
Copy link
Member

roji commented Apr 28, 2022

Submitted #27910 for patching this in 6.0. Manually verified that before the fix a minimal trimmed EF SQLite fails with the above error (missing DateOnly method), and after the fix the application works.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-sqlite closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported Servicing-approved type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants