LazyLoad no Virtual
See original GitHub issue@ajcvickers would like to know what impact this would cause. I’ve been working on something where not necessarily I would have “Virtual” type navigators, where today we are forced to declare everything as “Virtual” by activating LazyLoad. I made a little adjustment where everything works perfectly for me.
Here is an example:
public virtual InternalModelBuilder Apply(InternalModelBuilder modelBuilder)
{
if (_options?.UseLazyLoadingProxies == true)
{
foreach (var entityType in modelBuilder.Metadata.GetEntityTypes())
{
if (entityType.ClrType != null
&& !entityType.ClrType.IsAbstract
&& entityType.GetNavigations().Any(p => p.PropertyInfo.GetMethod.IsVirtual))
{
if (entityType.ClrType.IsSealed)
{
throw new InvalidOperationException(ProxiesStrings.ItsASeal(entityType.DisplayName()));
}
var proxyType = _proxyFactory.CreateLazyLoadingProxyType(entityType);
var serviceProperty = entityType.GetServiceProperties().FirstOrDefault(e => e.ClrType == typeof(ILazyLoader));
if (serviceProperty == null)
{
serviceProperty = entityType.AddServiceProperty(_lazyLoaderProperty, ConfigurationSource.Convention);
serviceProperty.SetParameterBinding(
(ServiceParameterBinding)new LazyLoaderParameterBindingFactory().Bind(
entityType,
typeof(ILazyLoader),
nameof(IProxyLazyLoader.LazyLoader)));
}
var binding = (ConstructorBinding)entityType[CoreAnnotationNames.ConstructorBinding];
if (binding == null)
{
_directBindingConvention.Apply(modelBuilder);
}
binding = (ConstructorBinding)entityType[CoreAnnotationNames.ConstructorBinding];
entityType[CoreAnnotationNames.ConstructorBinding]
= new FactoryMethodConstructorBinding(
_proxyFactory,
_createLazyLoadingProxyMethod,
new List<ParameterBinding>
{
new EntityTypeParameterBinding(),
new DefaultServiceParameterBinding(typeof(ILazyLoader), typeof(ILazyLoader), serviceProperty),
new ObjectArrayParameterBinding(binding.ParameterBindings)
},
proxyType);
foreach (var navigation in entityType.GetNavigations())
{
if (navigation.PropertyInfo == null)
{
throw new InvalidOperationException(
ProxiesStrings.FieldNavigation(navigation.Name, entityType.DisplayName()));
}
if (navigation.PropertyInfo.GetMethod.IsVirtual)
{
navigation.SetPropertyAccessMode(PropertyAccessMode.Field);
}
}
}
}
}
return modelBuilder;
}
I added just this:
entityType.GetNavigations().Any(p => p.PropertyInfo.GetMethod.IsVirtual))
And:
if (navigation.PropertyInfo.GetMethod.IsVirtual)
{
navigation.SetPropertyAccessMode(PropertyAccessMode.Field);
}
I believe that with this it would not be necessary to force the user to define a virtual type navigation property.
Thank you in advance.
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (7 by maintainers)
Top Results From Across the Web
NHibernate lazy loading but no virtual properties?
None of my properties in the entire model are marked as virtual because I dont want lazy loading. I'm mapping by code and...
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 >What is Lazy Loading | Lazy vs. Eager Loading
Lazy loading is an optimization technique that delays the loading of ... When the virtual object is called, load the real object, then...
Read more >EF Core doesn't lazy-load virtual navigation properties #3312
Issue: EF7 may not be lazy-loading all entities in a graph of related entities, despite all entities' navigation properties being virtual:.
Read more >Avoid Lazy Loading in ASP.NET - Shawn Wildermuth
Let's see what's wrong with Lazy Loading in Web Apps. ... Note the virtual property that is a relationship to the Address entity....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@ralmsdeveloper sure we could provide granular control at the navigation property level, assuming that is what you meant.
No offense taken 😄 Service and delegate injection give you complete control at the cost of more complexity in your entity types.
All right then, I’ll follow up on this issue https://github.com/aspnet/EntityFrameworkCore/issues/10787 Thanks!