Ho un progetto basato su ASP.NET Core 3.1 in cui sto usando Entity Framework Core 3.1 come ORM.
Ho i seguenti due modelli di entità
public class PropertyToList
{
public int Id { get; set; }
public int ListId { get; set; }
public int UserId { get; set; }
public int PropertyId { get; set; }
// ... Other properties removed for the sake of simplicity
public virtual Property Property { get; set; }
}
public class Property
{
public int Id { get; set; }
// ... Other properties removed for the sake of simplicity
public int TypeId { get; set; }
public int StatusId { get; set; }
public int CityId { get; set; }
public virtual Type Type { get; set; }
public virtual Status Status { get; set; }
public virtual City City { get; set; }
}
Sto cercando di interrogare tutte le proprietà a cui un utente ha relazione. L'oggetto PropertyToList
mi dice se un utente è correlato a una proprietà. Ecco cosa ho fatto
// I start the query at a relation object
IQueryable<Property> query = DataContext.PropertyToLists.Where(x => x.Selected == true)
.Where(x => x.UserId == userId && x.ListId == listId)
// After identifying the relations that I need,
// I only need to property object "which is a virtual property in" the relation object
.Select(x => x.Property)
// Here I am including relations from the Property virtual property which are virtual properties
// on the Property
.Include(x => x.City)
.Include(x => x.Type)
.Include(x => x.Status);
List<Property> properties = await query.ToListAsync();
Ma quel codice genera questo errore
Includi è stato utilizzato su entità non interrogabili
Cosa potrebbe causare questo problema? Come posso ripararlo?
Un'osservazione, entrambi i modelli di dominio PropertyToList e Property hanno proprietà virtuali. Inoltre, si utilizza l'operatore Includi per selezionare queste proprietà.
Questo non è necessario, quando la proprietà è definita con virtual, sarà caricata in modo pigro. Quindi non è necessario includere. Il caricamento lento non è il metodo consigliato, l'uso di include è migliore, quindi si selezionano solo le proprietà del grafico richieste.