I have a PlayerContext
model that has many BlueprintContext
models. Like so:
public class PlayerContext
{
public PlayerContext() { }
[Key]
public int id { get; set; }
...
public List<BlueprintContext> Blueprints { get; set; }
...
}
public class BlueprintContext
{
[Key]
public int id { get; set; }
public Dictionary<Coord3, Chonxels> BigData = new Dictionary<Coord3, Chonxels>() { };
public Dictionary<BlueprintIngredient, int> recipe = new Dictionary<BlueprintIngredient, int>();
public string Name { get; set; }
public int CreatorId { get; set; }
public PlayerContext Creator { get; set; }
}
In BlueprintContext
the BigData
field can get really big. So when I load a PlayerContext
I want the Blueprints
but I only want the id
, recipe
, and Name
fields (not the BigData
).
Is there a way to load the PlayerContext
and include the BlueprintContext
fields I need without the BigData
field?
Here is what I tried
using (var db = new EfContext())
{
PlayerContext playerContext = db.Players
.Where(p => p.id == playerId)
...
.Include(p => p.Blueprints.Select(b => new { b.id, b.recipe, b.Name}))
.AsNoTracking() // disables change tracking
.FirstOrDefault();
and I get this Exception:
System.InvalidOperationException: The expression 'p.Blueprints.AsQueryable().Select(b => new <>f__AnonymousType43`3(id = b.id, recipe = b.recipe, Name = b.Name))' is invalid inside an 'Include' operation, since it does not represent a property access: 't => t.MyProperty'. To target navigations declared on derived types, use casting ('t => ((Derived)t).MyProperty') or the 'as' operator ('t => (t as Derived).MyProperty'). Collection navigation access can be filtered by composing Where, OrderBy(Descending), ThenBy(Descending), Skip or Take operations. For more information on including related data, see http://go.microsoft.com/fwlink/?LinkID=746393.
I'm using Entity Framework Core.