I have an entity model with
class Package
{
...
virtual ICollection<Owner> Owners { get; set; }
}
and am implementing an AddOwner() operation.
Does the entity framework require me to retrieve my Package object including owners with .Include(p => p.Owners) in order for me to call p.Add(newOwner); and save changes back to the database?
Secondary question : Intuition isn’t guiding me to an answer here because I don’t know whether to base my intuition on CLR collections or on some principle of Entity Framework; is there a principle of EF that helps me understand things better than thinking in terms of CLR collections?
As long as your
Packageobject is attached to a context, theOwnersproperty will be set to anEntityCollection<Owner>. Regardless of whether it’s loaded, changes through that property will be marked as changes in your context, andSaveChanges()will save those changes.Keep in mind that your
Ownerobject already needs to be part of the context, if it is an existing owner, though. If it is not, EF will assume you are trying to save a newOwner.