更新实体框架中的 EntityKey 参考

发布于 2024-07-29 05:03:28 字数 1135 浏览 2 评论 0原文

我创建了一个使用 Asp.net、Entity Framework 和 Windows Workflow Foundation 的应用程序,一切都按预期工作。

我的 asp.net 页面应该启动一个工作流,该工作流执行并更新从页面传递到工作流的实体。 一切正常:我使用工作单元模式在 asp.net 和 WWF 之间共享上下文,并且我的实体在工作流程期间成功更新...除了在我的实体中建模为对另一个实体的引用的字段之外。

假设这个案例。 主实体称为 Item,它有一个称为 Status 的属性,该属性是另一个称为 Status 的实体的外键。 当我创建一个项目时,我只需使用通用语法创建它,

Item i = new Item();

然后创建对状态实体的引用,如下所示:

i.StatusReference.EntityKey = new System.Data.EntityKey("myEntities.StatusSet", "idStatus", State);

其中状态是在工作流程步骤中硬编码的整数值。

每当我尝试更新此实体(项目实体)时,我都会按照以下步骤操作: a) 我使用以下方法从上下文(一个新的实体,作为以下事务的工作单元构建)中检索实体:

Item i = (from item in ctx.ItemsSet where item.idItem == itemID select item).FirstOrDefault();

然后将实体附加到我的上下文。 EntityState 处于未更改状态

b) 我像以前一样更新状态引用:

i.StatusReference.EntityKey = new System.Data.EntityKey("myEntities.StatusSet", "idStatus", State);

c) 我保存对上下文的更改,例如: ctx.SaveChanges();

使用 vs2008 调试器,我可以看到更新的实体,当保存更改时...我看到所有属性都已更改(modifiedDate、itemName 和其他属性),但实体引用仍然指向原始属性。

有人可以给我一些帮助吗?

I created an application that uses Asp.net, Entity Framework and Windows Workflow Foundation and everything works as expected.

My asp.net page should start a Workflow that executes and update an entity passed to the workflow from the page. Everything works ok: i used Unit Of Work pattern to share the context between asp.net and WWF and my entity is successfully updated during the workflow... except a field that is modelled in my entity as a reference to another entity.

Suppose this case. The main entity is called Item that has a property called Status that is a foreign key to another entity called Status. When i create an Item, i just create it with the common syntax like

Item i = new Item();

I then create the reference to Status entity like this:

i.StatusReference.EntityKey = new System.Data.EntityKey("myEntities.StatusSet", "idStatus", State);

where State is an integer value hardcoded in the Workflow step.

Whenever I try to update this Entity (the Item one) i follow these steps:
a) I retrieve the Entity from the context (a new one, built as Unit Of Work for the following transaction) using:

Item i = (from item in ctx.ItemsSet where item.idItem == itemID select item).FirstOrDefault();

Entity is then attached to my context. EntityState is in UNCHANGED

b) I update the Status reference like before:

i.StatusReference.EntityKey = new System.Data.EntityKey("myEntities.StatusSet", "idStatus", State);

c) I save changes on the context like:
ctx.SaveChanges();

Using vs2008 debugger i can see the updated entity and when changes are saved... i see all properties changed (modifiedDate, itemName and other props) but entity reference still points at the original one.

Can anyone give me some help?!

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文