Closed CheloXL closed 1 month ago
@cincuranet is he returning?
@stevendarby Generally, it should work as you expect. Could you share a small runnable project that shows the situation where only one schema is loaded?
@AndriySvyryd cc @CheloXL
@stevendarby, @AndriySvyryd Here you have a small reproducible test. As you can see in the console, EF never queries for the TableSchemas collection and logs that it founds one table, when it should have log 2 entries.
@CheloXL The problem is in the extension method. You need to remove the if
:
[return: NotNullIfNotNull(nameof(navigationField))]
public static TRelated? Load<TRelated>(this Action<object, string>? loader, object entity, ref TRelated? navigationField, [CallerMemberName] string navigationName = null!)
where TRelated : class
{
loader?.Invoke(entity, navigationName);
return navigationField;
}
I have the below code (very simplified), and configured everything via code configuration (
IEntityTypeConfiguration<>
). Table schema is configured withbuilder.HasOne(x => x.DatabaseSchema).WithMany(x => x.TableSchemas);
.Now, I load a specific TableSchema by Id, then access its sibling table schemas by doing
tableSchema.DatabaseSchema.TableSchemas
. That only returns one table schema, the one I just loaded. It seems that the lazy loader doesn't go to the database to retrieve all the related table schemas.I think I may understand why this is happening, but I need a way for this to work without having to auto-include the
TableSchemas
navigation property. That access pattern (accessing the sibling table schemas) is not something that happens a lot (that's why it's configured as lazy).Is there a way to tell the lazyLoader that I really want that collection to be loaded from DB? I could handle then myself the reference to the loaded collection so my code doesn't keep calling the loader if I already loaded it from DB.