(Entity Framework 4.0, with POCO template)
I adding an object through the data context using the standard approach:
_model.Products.AddObject(objNewProduct);
_model.SaveChanges();
However, the object has a number of related objects attached as properties. For example there is a Category object attached to objNewProduct.
When SaveChanges() is called, EF attempts to insert a new Category record into the database, but as the category already exists, this is rejected by the database.
objNewProduct was passed into the application through a web service, rather than being created in code as a new product. I can't do anything about the fact that it has extra objects attached to it. I don't know if this is the source of the problem, or if as
a general rule objects being added to the data context should never have related objects attached.
So, is a better approach to create a new product object, transfer the necessary properties from the passed in object to it, then add it and save changes? Or is there a less laborious option to saving an object from an external source?