[英]EF Core Populate a "NotMapped property" from a not included List of entities
I have two entities like these我有两个这样的实体
public class PetShop
{
public int Id { get; set; }
public string Title { get; set; }
public IList<Cat> Cats { get; set; }
[NotMapped]
public Cat OldestCat {
get => Cats.OrderBy(c => c.BirthDate).FirstOrDefault();
}
}
public class Cat
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime BirthDate { get; set; }
}
Normally to get the PetShop with all the cats, this is the query I have to make with my DbContext:通常要获得所有猫的 PetShop,这是我必须使用 DbContext 进行的查询:
DbContext
.PetShop
.Include(a => a.Cats);
But sometimes I do not want to include all the cats of my PetShops, since I could have a lot of cats, but I still need to populate my OldestCat, but without including the cats, my getter will return me nothing.但有时我不想包括我的 PetShops 的所有猫,因为我可以有很多猫,但我仍然需要填充我的 OldestCat,但是如果不包括猫,我的 getter 将不会返回任何东西。
Is there a way to populate my NotMapped OldestCat property without including all my cats ?有没有办法在不包括我所有的猫的情况下填充我的 NotMapped OldestCat 属性?
Edit.编辑。 I changed the answer.我改变了答案。 Mabye the easiest would be to make a link from cat to petshop and then begin with the Cat and from that Select the petshop? Mabye 最简单的方法是建立从 cat 到 petshop 的链接,然后从 Cat 开始,然后从中选择 petshop?
I have not tested the code.我没有测试代码。
DbContext
.Cats
.Where(c => c.PetShop.Id == 5)
.OrderBy(c => c.BirthDate)
.Select(c => c.PetShop)
.FirstOrDefault()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.