Here are a few things going on.
1 If you are configured for lazy loading, child objects are loaded only if you tell them to load. This can be done using the following in your request.
..
context.Contacts.Include(c => c.ContactInfos).Include(c => c.ContactInfos.ContactInfoType)
see this article for full details on how objects are loaded as you like.
2 If you do not want to save contactinfo and contactinfotype (because they are not loaded or you just do not want to), you will need to tell the context not to save child objects that should not be updated. This can be done using:
..
context.StateManager.ChangeObjectState(entity.ContactInfos.ContactInfoType, EntityState.Unchanged);
I find that I need to do this when changing / using a country object for user data. I definitely do not want this to be updated by the user.
In the middle of writing a bit of guidance on all of this, but it may be weeks until this is done on my blog.
3 MVC will not store / send back everything that you do not put on the form. If you submit a hierarchy object to a form and the values are not represented in hidden entries, they are returned to your model empty. For this reason, I generally do viewmodels that are edited only by versions of objects with ToEntity and the ToModel method. It also covers me for security, because I don’t want all kinds of user IDs to be in hidden inputs, so that my entities are displayed directly in MVC (see this article on surge ).
I would think that you have your contactinfo properties set to virtual, UpdateModel doesn’t mind if they did not exist when returning, but I could have been mistaken since I didn’t try.
Gats
source share