gpt4 book ai didi

.net - 使用 EF4 和 mVC2 的 self 跟踪实体 (STE) 和部分更新

转载 作者:行者123 更新时间:2023-12-02 02:35:16 26 4
gpt4 key购买 nike

我有一个 MVC2 应用程序,我开始使用 STE的。我正在寻找有关更新应如何工作的说明。

背景:

如果我有一个包含相关类别实体和相关帖子/评论实体的博客实体。在 MVC 中,我正在呈现一个包含主要博客实体和类别但不包含相关帖子的 View 。当我将博客实体发回服务器时,我可以看到相关类别,但看不到正在序列化回服务器的实体中的帖子(因为它们不在 View 中)。此外,博客实体的更改状态为“已添加”。

然后我尝试调用此 Blog 实体上的 Applychanges()SaveChanges() 来执行更新,但由于与帖子的 FK 关系和事实上,数据库中有相关帖子,但没有附加到我发回的实体。

通过一些进一步的测试...如果我在连接到服务器(state=unchanged)时获取 Blog 实体的当前实例(以及所有相关的 FK 实体),修改属性(state=Modified)并更新它按预期工作。

所以我的问题:如果我有未在 View 中呈现的相关实体,因此不会与博客实体一起回发,更新是否应该起作用?

为什么博客实体返回状态为“已添加”而不是“已修改”?我假设它会为所有已更改的实体返回一个“已修改”更改状态,然后当我调用 ApplyChanges/SaveChanges() 时,只有修改的项目会尝试更新,这就是我不需要的原因所有相关实体。

我应该能够直接从客户端和 ApplyChanges()/SaveChanges() 传递实体,还是应该将实体回发到服务器,从数据库中获取现有副本,对该副本应用更改,然后将现有对象发回?

最佳答案

关于你的第一个问题:

I then try to call Applychanges() and SaveChanges() on this Blog entity to the perform an update and it fails because of the FK relationship with posts and the fact that there are related posts in the database but not attached to the entity I am sending back.

这可能是由您的第二个问题引起的(“已添加”状态而不是“已修改”)。上下文可能正在尝试添加博客而不是尝试保存更改,因此出现 FK 约束错误。

关于你的第二个问题:

Why does the Blog entity get post back with an 'Added' status and not 'Modified'? I would assume it would come back with a 'Modified' changedstate for all changed entities and then when I called ApplyChanges/SaveChanges() only the modified items would attempt to update and this is why I would not need all related entities.

要考虑的问题是“您是否在服务器和客户端上使用相同类型的生成代码”? STE已known to not keep track of changes当您使用数据服务元数据生成的类型时。对于 STE,有一段重要的代码在构造函数中被激活,它不会通过元数据生成的类型持续存在,因此是导致此问题的原因。

至于你的最后一个问题:

Should I be able to pass an entity directly from the client and ApplyChanges()/SaveChanges() or should I be posting back to the server the entity, grabbing an existing copy from the database, applying changes to that copy and then posting the existing object back?

是的,有可能(我已经测试过)将 ApplyChanges()/SaveChanges() 应用到直接来自客户端的实体,而不管实体最初来自哪里(从服务器到客户端修改或创建作为要添加的新实体)。

关于.net - 使用 EF4 和 mVC2 的 self 跟踪实体 (STE) 和部分更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2118355/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com