Reputation: 480
I have a model created with EF Core (code first), and one of my properties tries to access a lazy loaded proxy, as below:
public class T012_Pedido
{
private string _t012NumeroPedido;
private ObservableCollection<T012_Pedido_Item> _t012PedidoItem = new();
[Required]
[Column(TypeName = "varchar")]
[MaxLength(16)]
public string T012_NumeroPedido
{
get => this._t012NumeroPedido;
set
{
if (value == this._t012NumeroPedido)
{
return;
}
this._t012NumeroPedido = value;
this.OnPropertyChanged();
}
}
[BackingField(nameof(_t012PedidoItem))]
public ObservableCollection<T012_Pedido_Item> T012_Pedido_Item
{
get => this.LazyLoader.Load(this, ref this._t012PedidoItem);
set => this._t012PedidoItem = value;
}
}
public class T012_Pedido_Item
{
[Required]
[BackingField(nameof(_t012Pedido))]
public virtual T012_Pedido T012_Pedido
{
get => this.LazyLoader.Load(this, ref this._t012Pedido);
set
{
if (this._t012Pedido == value)
{
return;
}
this._t012Pedido = value;
this.OnPropertyChanged();
}
}
public decimal T012_PesoLiquido
{
get => this._t012PesoLiquido;
set
{
this._t012PesoLiquido = value;
this.OnPropertyChanged();
}
}
}
If I try to access the property T012_Pedido in the property T012_PesoLiquido on class T012_Pedido_Item, I get the following error:
System.InvalidOperationException: An error was generated for warning 'Microsoft.EntityFrameworkCore.Infrastructure.DetachedLazyLoadingWarning': An attempt was made to lazy-load navigation 'T012_Pedido' on a detached entity of type 'T012_Pedido_Item'. Lazy loading is not supported for detached entities or entities that are loaded with 'AsNoTracking'. This exception can be suppressed or logged by passing event ID 'CoreEventId.DetachedLazyLoadingWarning' to the 'ConfigureWarnings' method in 'DbContext.OnConfiguring' or 'A ddDbContext'.
If I don't have any code accessing the property inside the model, and try to access in other block of code, everything works. I think that when I try to access inside the property T012_PesoLiquido the lazy loading is not ready, and that's why I get the error. I would like to know how to do in this case.
Thanks in advance.
EDIT:
I've found out that if I try to access the field instead of the property, the error does not occur and I'm able to access all the data.
Upvotes: 0
Views: 1065
Reputation: 34758
That error will occur whenever you explicitly or implicitly attempt to lazy-load an entity outside of the scope of the DbContext that it is associated to. "Checking if it's lazy loaded or not" really just amounts to turning off lazy loading, in the sense that you'll either get the entity (if it had been loaded prior to leaving the scope of the DbContext) or you will get #null.
Essentially if you do this: "This exception can be suppressed or logged by passing event ID 'CoreEventId.DetachedLazyLoadingWarning' to the 'ConfigureWarnings' method in 'DbContext.OnConfiguring' or 'A ddDbContext'." on the DbContext configuration, it should return #null if the lazy load cannot be done rather than throw that exception.
If you are going to use lazy loading it is important to ensure that an entity is always accessed while within the scope of a DbContext
. Either the DbContext
where it was read, or having been detached from that context and attached to a new DbContext
instance that it can call upon to perform any possible lazy loading.
Upvotes: 1