Entity Framework 6: Adding child object to parent's list vs. setting child's navigation property to parent Entity Framework 6: Adding child object to parent's list vs. setting child's navigation property to parent asp.net asp.net

Entity Framework 6: Adding child object to parent's list vs. setting child's navigation property to parent


Yes, the two approaches are interchangeable. This allows you to create and save your object graph to the database from either the perspective of the MailServer or the MailDomain.

If you do code-first, you have the option of removing the properties and mappings if they're not needed.

I also assume that in approach (A) the new MailDomain instance is automatically added to context.MailDomains while in approach (B) the new MailServer instance is automatically added to context.MailServers. Is that correct or do I have to do that manually?

It depends what you mean by "added to the context". If you mean: does it automatically get saved to the database when you persist, the answer is yes. One of the big benefits to using an ORM like EF is that it handles saving a full object graph automatically (and syncing PK/FK relations, etc.).

If you mean: will the entity be available via the context before saving, I don't think so (I'm not 100% sure).