Entity Framework Core 3.1을 ORM으로 사용하는 ASP.NET Core 3.1 기반 프로젝트가 있습니다.
다음 두 가지 엔티티 모델이 있습니다
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; }
}
사용자가 관련된 모든 속성을 쿼리하려고합니다. PropertyToList
객체는 사용자가 속성과 관련되어 있는지 알려줍니다. 여기 내가 한 일이 있습니다
// 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();
그러나 그 코드는이 오류를 던지고 있습니다.
엔티티가 아닌 조회 가능에 포함이 사용되었습니다.
이 문제의 원인은 무엇입니까? 어떻게 고칠 수 있습니까?
관찰 결과, 도메인 모델 PropertyToList 및 Property에는 모두 가상 속성이 있습니다. 또한 포함 연산자를 사용하여 이러한 속성을 선택하고 있습니다.
속성이 virtual로 정의 된 경우에는 필요하지 않으며 지연로드됩니다. 따라서 포함이 필요하지 않습니다. 게으른 로딩은 권장되는 방법이 아니며 include를 사용하는 것이 더 좋으므로 필요한 그래프 속성 만 선택하십시오.