I am loading an entity (DataLayer
) from a database using Entity Framework. This entity has a reference in its model class to another entity (DataLayerStyle
) as a list Styles
.
This second entity also references back to the original entity (DataLayer
). So, when I load the original entity, it gets loaded in the second entity (DataLayerStyle
) in a recursive fashion. I don't want to do that in this particular instance because the data size that is transferred from server to client is too big and takes too long for the client to load.
I have tried using Select to only select certain parameters in the second entity's class before loading all of the original entity into a variable, but I get an error:
Lambda expression used inside Include is not valid
This is also not really the ideal solution, because each layer has a different style class, so when I tried to use Select in this way, I was only limited to the most generic version of the style class.
var list = dbContext.DataLayer.Where(x => x.LayerGroupId == groupId)
.Include(x => x.Permissions)
.Include(x => x.LayerGroup)
.ThenInclude(x => x.DataManager)
.ThenInclude(x => x.Project)
.Include(x => x.Styles
.Select(x => new { x.Visible, x.ImageURL, x.DataLayerId, x.DataLayerStyleId, x.UserId }))
.OrderBy(x => x.Name).AsNoTracking().ToList();
I also tried adding in AsNoTracking()
but that did not help, either.
I am looking for something that would be the opposite of Include, like DoNotInclude
, but that does not seem to exist.
public class DataLayer
{
#region Properties
public int DataLayerId { get; set; }
public virtual LayerGroup LayerGroup { get; set; }
public int LayerGroupId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public DateTime DateCreated { get; set; }
public string DataLayerType { get; set; }
public virtual DataLayerDocumentManager DocumentManager { get; set; }
public virtual List<DataFeature> DataFeatures { get; set; } = new List<DataFeature>();
public virtual List<DataItemSummary> DataItemSummaries { get; set; } = new List<DataItemSummary>();
public virtual List<DataField> DataFields { get; set; } = new List<DataField>();
public virtual List<DataLayerStyle> Styles { get; set; } = new List<DataLayerStyle>();
public virtual List<DataLayerPermission> Permissions { get; set; } = new List<DataLayerPermission>();
public virtual List<DataLayerTag> Tags { get; set; } = new List<DataLayerTag>();
public virtual List<DataLayerLogEntry> LogEntries { get; set; } = new List<DataLayerLogEntry>();
public string UserId { get; set; }
#endregion
}
public class DataLayerStyle
{
#region Properties
public int DataLayerStyleId { get; set; }
public virtual DataLayer DataLayer { get; set; }
public int DataLayerId { get; set; }
public bool Visible { get; set; } = false;
public string UserId { get; set; }
public string ImageURL { get; set; }
#endregion
}
var list = dbContext.DataLayer.Where(x => x.LayerGroupId == groupId)
.Include(x => x.Permissions)
.Include(x => x.LayerGroup).ThenInclude(x => x.DataManager).ThenInclude(x => x.Project)
.Include(x => x.Styles)
.OrderBy(x => x.Name).AsNoTracking().ToList();