Let's assume this scenario
Entity
public virtual List<Address> AddressHistory { get; set; }
public Address Address
{
get
{
if (AddressHistory.Any())
{
return AddressHistory.OrderByDescending(x => x.CreationDate).FirstOrDefault();
}
return null;
}
}
When requesting an address based on a condition, like this :
dbContext.MyEntity.Where(e => e.Address.Street == "some stuff");
I get a null reference exception.
Why ? Is there a way to make it work ?
EDIT: for those who think Address might be empty, it works when doing that :
dbContext.MyEntity.Where(e => e.AddressHistory.OrderByDescending(x => x.CreationDate).FirstOrDefault().Street == "some stuff");
EDIT: To the guy who marked it as duplicate, I don't think you understand the issue here. Please remove the marking.
So, to sum up :
If I use the getter => null exception, because the children (AdressHistory) is not lazy loaded. If I use the code inside the getters directly in the efcore expression, it works.
Which means using a getter doesn't work in EFCore.