Pokaż konfigurację relacji. Może korzystasz z metody
OwnsMany()
W najnowszych ef core'ach ( nie wiem od której wersji) jeśli korzystasz w konfiguracji metody OwnsMany to wtedy masz domyślnie eager loading dla tych encji - ostatnio się na tym złapałem.
Co ciekawe tak to działa w kodzie a w Unit Testach gdy robiłem bazę InMemory to nie do końca to tak działało :D
InMemory to w ogóle nie zachowuje relacji i działa jak chce... Dlatego przerzuciłem się na testcontainers z obrazem dockerowym SqlServera.
internal sealed class ActionConfiguration : IEntityTypeConfiguration<Action>
{
public void Configure(EntityTypeBuilder<Action> builder)
{
builder.GenerateBasePropertiesRules();
builder.Property(x => x.GoalId)
.HasConversion(x => x.Value, x => new BaseId(x))
.IsRequired();
builder.Property(x => x.ActionId)
.HasConversion(x => x.Value, x => new BaseId(x))
.IsRequired();
builder.ComplexProperty(x => x.Name, cpb =>
{
cpb.Property(x => x.Value)
.HasColumnName(nameof(Action.Name))
.HasMaxLength(250)
.IsRequired();
cpb.IsRequired();
});
builder.HasOne(x => x.Creator)
.WithMany(x => x.ActionCreators)
.HasForeignKey(x => x.CreatorId)
.IsRequired();
builder.HasOne(x => x.Modificator)
.WithMany(x => x.ActionModificators)
.HasForeignKey(x => x.ModificatorId)
.IsRequired(false);
builder.HasOne(x => x.CreatorContext)
.WithMany(x => x.ReportActionCreatorsContexts)
.HasForeignKey(x => x.CreatorContextId)
.IsRequired(false);
builder.HasOne(x => x.ModificatorContext)
.WithMany(x => x.ReportActionModificatorsContexts)
.HasForeignKey(x => x.ModificatorContextId)
.IsRequired(false);
builder.HasOne(x => x.Goal)
.WithMany(x => x.Actions)
.HasForeignKey(x => x.GoalId)
.IsRequired();
builder.HasOne(x => x.Action)
.WithMany(x => x.Actions)
.HasForeignKey(x => x.ActionId)
.IsRequired();
}
}
Zastanawia mnie też jedna kwestia...
Czemu ChangeTracker ma ustawione LazyLoadingEnabled na true skoro EagerLaoding jest domyślnym zachowaniem EF?
Sprawdziłem w projekcie gdzie jest .NET 6 i tam też ChangeTracker posiada ustawione LazyLoadingEnabled na true.
Natomiast sam MS wskazuje, że aby LazyLoading działało to trzeba zastosować paczkę Microsoft.EntityFrameworkCore.Proxies.
https://learn.microsoft.com/en-us/ef/core/querying/related-data/lazy