Update EntityKey Reference in Entity Framework

那年仲夏 提交于 2019-12-10 18:49:14

问题


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?!

来源:https://stackoverflow.com/questions/1178885/update-entitykey-reference-in-entity-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!