EF Changetracker可以告诉您最初是否有一个收藏品?

发布于 2025-02-10 04:48:31 字数 341 浏览 4 评论 0原文

考虑以下简单情况。

class Adult
{
    public List<Children> Children { get; private set; }
}

如果您通过了由实体框架跟踪的父母实例,并且此实例有一个空的儿童列表,您是否可以通过编程方式确定孩子的收集是否为空,因为父母实际上没有孩子或是否有孩子空列表是由于这些数据没有被获取吗? 我认为EF具有某种形式的知识,因为我们应该能够在不取孩子并更改父母的情况下检索父母。名称等,并调用savechanges而无需删除EF的savechanges在保存时为空。

我已经研究了跟踪性和实体,但我看不到如何做出这一决定。

Consider the following simple case.

class Adult
{
    public List<Children> Children { get; private set; }
}

If you are passed an instance of Parent that is tracked by Entity Framework and this instance has an empty list of children, is there any way that you can programatically determine if the Children collection is empty due to the parent actually not having children or if the empty list is due to these data not being fetched?
I assume that EF has some form of knowledge of it, since we are supposed to be able to retrieve a parent without fetching the children and make changes to Parent.Name and such and invoke SaveChanges without EF deleting the children just because the Children collection is empty at the time of save.

I've looked at TrackedEntities and EntityStates, but I don't see how to make this determination.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无戏配角 2025-02-17 04:48:31
var isLoaded = context.Entry(adult).Collection(p => p.Children).IsLoaded;

context.Entry(adult).Collection(p => p.Children).Load();
var isLoaded = context.Entry(adult).Collection(p => p.Children).IsLoaded;

and

context.Entry(adult).Collection(p => p.Children).Load();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文