Does UnitTest Project Support EF Core Lazy Loading?
See original GitHub issueI want to let Unit Test project support lazy loading feature which introduced in EF Core 2.1. Ref : https://docs.microsoft.com/en-us/ef/core/querying/related-data#lazy-loading
So, I Performed following steps try to reach the goal: Step1. Install-Package Microsoft.EntityFrameworkCore.Proxies on Unit Test Project. Step2. Add services.AddEntityFrameworkProxies(); statement in function Register(IIocManager iocManager) of ServiceCollectionRegistrar.cs Step3. In same file, add use lazy loading proxy like this : builder.UseLazyLoadingProxies().UseInMemoryDatabase(Guid.NewGuid().ToString()).UseInternalServiceProvider(serviceProvider); ABP package version: 4.0.1 Base on .Net Core
When I ran the test case, I got this exception message: Castle.MicroKernel.ComponentActivator.ComponentActivatorException : Factory method creating instances of component ‘Microsoft.EntityFrameworkCore.Infrastructure.IDbContextOptions_d4040eea-3d91-4e91-aee4-454219d15a92’ returned null. This is not allowed and most likely a bug in the factory method.
Any idea or suggestion about this problem? Thanks in advance!
PS:
Result StackTrace:
at Castle.MicroKernel.ComponentActivator.FactoryMethodActivator`1.Instantiate(CreationContext context)
at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.InternalCreate(CreationContext context)
at Castle.MicroKernel.ComponentActivator.AbstractComponentActivator.Create(CreationContext context, Burden burden)
at Castle.MicroKernel.Lifestyle.AbstractLifestyleManager.CreateInstance(CreationContext context, Boolean trackedExternally)
at Castle.MicroKernel.Lifestyle.ScopedLifestyleManager.<>c__DisplayClass4_0.<Resolve>b__0(Action`1 afterCreated)
at Castle.MicroKernel.Lifestyle.Scoped.DefaultLifetimeScope.GetCachedInstance(ComponentModel model, ScopedInstanceActivationCallback createInstance)
at Castle.MicroKernel.Lifestyle.ScopedLifestyleManager.Resolve(CreationContext context, IReleasePolicy releasePolicy)
at Castle.Windsor.MsDependencyInjection.MsScopedLifestyleManager.Resolve(CreationContext context, IReleasePolicy releasePolicy) in D:\Github\castle-windsor-ms-adapter\src\Castle.Windsor.MsDependencyInjection\MsScopedLifestyleManager.cs:line 21
at Castle.MicroKernel.Handlers.DefaultHandler.ResolveCore(CreationContext context, Boolean requiresDecommission, Boolean instanceRequired, Burden& burden)
at Castle.MicroKernel.Handlers.DefaultHandler.Resolve(CreationContext context, Boolean instanceRequired)
at Castle.MicroKernel.Resolvers.DefaultDependencyResolver.Resolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model, DependencyModel dependency)
at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.CreateConstructorArguments(ConstructorCandidate constructor, CreationContext context)
at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.Instantiate(CreationContext context)
at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.InternalCreate(CreationContext context)
at Castle.MicroKernel.ComponentActivator.AbstractComponentActivator.Create(CreationContext context, Burden burden)
at Castle.MicroKernel.Lifestyle.AbstractLifestyleManager.CreateInstance(CreationContext context, Boolean trackedExternally)
at Castle.MicroKernel.Lifestyle.ScopedLifestyleManager.<>c__DisplayClass4_0.<Resolve>b__0(Action`1 afterCreated)
at Castle.MicroKernel.Lifestyle.Scoped.DefaultLifetimeScope.GetCachedInstance(ComponentModel model, ScopedInstanceActivationCallback createInstance)
at Castle.MicroKernel.Lifestyle.ScopedLifestyleManager.Resolve(CreationContext context, IReleasePolicy releasePolicy)
at Castle.Windsor.MsDependencyInjection.MsScopedLifestyleManager.Resolve(CreationContext context, IReleasePolicy releasePolicy) in D:\Github\castle-windsor-ms-adapter\src\Castle.Windsor.MsDependencyInjection\MsScopedLifestyleManager.cs:line 21
at Castle.MicroKernel.Handlers.DefaultHandler.ResolveCore(CreationContext context, Boolean requiresDecommission, Boolean instanceRequired, Burden& burden)
at Castle.MicroKernel.Handlers.DefaultHandler.Resolve(CreationContext context, Boolean instanceRequired)
at Castle.MicroKernel.DefaultKernel.ResolveComponent(IHandler handler, Type service, IDictionary additionalArguments, IReleasePolicy policy)
at Castle.MicroKernel.DefaultKernel.Castle.MicroKernel.IKernelInternal.ResolveAll(Type service, IDictionary arguments, IReleasePolicy policy)
at Castle.Windsor.MsDependencyInjection.ScopedWindsorServiceProvider.ResolveInstanceOrNull(Type serviceType, Boolean isOptional) in D:\Github\castle-windsor-ms-adapter\src\Castle.Windsor.MsDependencyInjection\ScopedWindsorServiceProvider.cs:line 86
at Castle.Windsor.MsDependencyInjection.ScopedWindsorServiceProvider.GetServiceInternal(Type serviceType, Boolean isOptional) in D:\Github\castle-windsor-ms-adapter\src\Castle.Windsor.MsDependencyInjection\ScopedWindsorServiceProvider.cs:line 55
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider provider)
at Microsoft.EntityFrameworkCore.Proxies.Internal.ProxiesOptionsExtension.Validate(IDbContextOptions options)
at Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.GetOrAdd(IDbContextOptions options, Boolean providerRequired)
at Microsoft.EntityFrameworkCore.DbContext..ctor(DbContextOptions options)
at Abp.Zero.EntityFrameworkCore.AbpZeroDbContext`4..ctor(DbContextOptions`1 options) in D:\Github\aspnetboilerplate\src\Abp.ZeroCore.EntityFrameworkCore\Zero\EntityFrameworkCore\AbpZeroDbContext.cs:line 68
at AbpTest.EntityFrameworkCore.AbpTestDbContext..ctor(DbContextOptions`1 options) in C:\Workspace\Lab\4.0.1\aspnet-core\src\AbpTest.EntityFrameworkCore\EntityFrameworkCore\AbpTestDbContext.cs:line 14
at lambda_method(Closure , Object[] )
at Castle.Core.Internal.ReflectionUtil.Instantiate[TBase](Type subtypeofTBase, Object[] ctorArgs)
at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.CreateInstanceCore(ConstructorCandidate constructor, Object[] arguments, Type implType)
at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.CreateInstance(CreationContext context, ConstructorCandidate constructor, Object[] arguments)
at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.InternalCreate(CreationContext context)
at Castle.MicroKernel.ComponentActivator.AbstractComponentActivator.Create(CreationContext context, Burden burden)
at Castle.MicroKernel.Lifestyle.AbstractLifestyleManager.CreateInstance(CreationContext context, Boolean trackedExternally)
at Castle.MicroKernel.Lifestyle.AbstractLifestyleManager.Resolve(CreationContext context, IReleasePolicy releasePolicy)
at Castle.MicroKernel.Handlers.DefaultHandler.ResolveCore(CreationContext context, Boolean requiresDecommission, Boolean instanceRequired, Burden& burden)
at Castle.MicroKernel.Handlers.DefaultHandler.Resolve(CreationContext context, Boolean instanceRequired)
at Castle.MicroKernel.DefaultKernel.ResolveComponent(IHandler handler, Type service, IDictionary additionalArguments, IReleasePolicy policy)
at Castle.MicroKernel.DefaultKernel.Castle.MicroKernel.IKernelInternal.Resolve(Type service, IDictionary arguments, IReleasePolicy policy)
at Castle.Windsor.WindsorContainer.Resolve[T]()
at AbpTest.Tests.AbpTestTestBase.UsingDbContext(Nullable`1 tenantId, Action`1 action) in C:\Workspace\Lab\4.0.1\aspnet-core\test\AbpTest.Tests\AbpTestTestBase.cs:line 84
at AbpTest.Tests.AbpTestTestBase.UsingDbContext(Action`1 action) in C:\Workspace\Lab\4.0.1\aspnet-core\test\AbpTest.Tests\AbpTestTestBase.cs:line 62
at AbpTest.Tests.AbpTestTestBase..ctor() in C:\Workspace\Lab\4.0.1\aspnet-core\test\AbpTest.Tests\AbpTestTestBase.cs:line 33
at AbpTest.Tests.Sessions.SessionAppService_Tests..ctor() in C:\Workspace\Lab\4.0.1\aspnet-core\test\AbpTest.Tests\Sessions\SessionAppService_Tests.cs:line 12
Result Message: Castle.MicroKernel.ComponentActivator.ComponentActivatorException : Factory method creating instances of component 'Microsoft.EntityFrameworkCore.Infrastructure.IDbContextOptions_d4040eea-3d91-4e91-aee4-454219d15a92' returned null. This is not allowed and most likely a bug in the factory method.
Issue Analytics
- State:
- Created 5 years ago
- Comments:12 (6 by maintainers)
Top GitHub Comments
Try calling
UseLazyLoadingProxies()
in*DbContextConfigurer
instead.I also changed test cases to use lazy loading, they are all passed. Thank you for your work! @maliming