【发布时间】:2022-01-23 23:08:08
【问题描述】:
我有一个 PlayerContext 模型,其中包含许多 BlueprintContext 模型。像这样:
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; }
}
在BlueprintContext 中,BigData 字段可以变得非常大。因此,当我加载PlayerContext 时,我想要Blueprints,但我只想要id、recipe 和Name 字段(而不是BigData)。
有没有办法加载PlayerContext 并在没有BigData 字段的情况下包含我需要的BlueprintContext 字段?
这是我尝试过的
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();
我得到了这个异常:
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.
我正在使用 Entity Framework Core。
【问题讨论】:
标签: entity-framework entity-framework-core