How do I update an Entity Reference using a SelectList in edit form? (asp.net mvc)
Just trying to figure out how do achieve the f开发者_如何学Goollowing:
I have an edit form, and a simple viewmodel for said form. The viewmodel contains an entity and then a few SelectLists for dropdowns.
The problem is this: The entity in question has a related entity (which is called room). I want the user to be able to select a different room from a dropdown and have this related entity changed accordingly.
I cant just set the RoomID to a new value and the entity framework doesn't expose it. If I try then UpdateModel fails.
So, is is possible to update an entity reference via an edit form?
Thanks in Advance
(starting to wish I'd used Linq to SQL)
(Edited for clarity)
We do it by just updating the ID (more or less; we use presentation models instead of binding our views directly to entities, but you get the idea...):
SomeEntity.SomeOtherEntityReference.EntityKey =
new EntityKey("MyEntities.SomeOtherEntities", "Id",
presentationModel.SomeOtherEntityId);
EF 4 makes this more elegant, yes, but you can certainly do it in EF 1, as well.
精彩评论