How can I add an entity to the database through EF4 without attaching all of its referenced entities first?
var entity = new MyEntity() { FK_ID = 4 }; // associated entity key
using (var db = new MyEntities())
{
db.MyEntity.AddObject(entity);
db.SaveChanges();
db.AcceptAllChanges();
}
This code keeps trying to also insert a new default-valued FK_Entity.
I see some suggestions online that I need to attach FK_Entity first and then set the MyEntity.FK property to the attached FK_Entity, but that seems awful. 1) I assume attaching requires loading the FK_Entity, which I don't need just to insert the entity - I already gave it the right key and SQL will enforce referential integrity if I make a mistake. 2) As I have more references, I have to attach each one??
I can't find any options or overloads for supressing cascaded inserts. I must be thinking about this wrong?
Thanks.