question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Does UnitTest Project Support EF Core Lazy Loading?

See original GitHub issue

I 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:closed
  • Created 5 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
acjhcommented, Nov 4, 2018

Try calling UseLazyLoadingProxies() in *DbContextConfigurer instead.

public static void Configure(DbContextOptionsBuilder<AbpProjectNameDbContext> builder, ...)
{
    builder
        .UseLazyLoadingProxies()
        .UseSqlServer(...);
}
0reactions
OverwindSlashcommented, Dec 12, 2019

I also changed test cases to use lazy loading, they are all passed. Thank you for your work! @maliming

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to unit test .Include for Entity Framework eager loading
2. Short Answer: You can't. ·.Include has been unit tested for you :) · 1. If you would be able to mock it,...
Read more >
Lazy Loading of Related Data - EF Core
EF Core will then enable lazy loading for any navigation property that can be overridden--that is, it must be virtual and on a...
Read more >
Avoid Lazy Loading in ASP.NET - Shawn Wildermuth
Luckily, Entity Framework Core has made it harder to inadvertently turn it on. Let's see what's wrong with Lazy Loading in Web Apps....
Read more >
Best Practices in Using the DbContext in EF Core
Entity Framework Core uses any of the following three approaches to load related entities in your application. ... You should turn off lazy...
Read more >
Lazy Loading Related Data In Entity Framework Core
Lazy loading of data is a pattern whereby the retrieval of data from the database is deferred until it is needed.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found