I'm trying to create a method on an EF 4 POCO repository called AddOrUpdate.
The idea is that the business layer can pass in a POCO object and the persistence framework will add the object if it is new (not yet in the database), else will update the database (once SaveChanges() is called) with the new value. This is similar to some other questions I have asked about EF, but I'm only about 80% there in understanding this so please forgive partial duplication.
The part I'm missing is how to update the object graph in my ObjectContext/associated ObjectSet for the passed-in business object once I have determined that the business object indeed already exists in the database (and now has been loaded thanks to TryGetObjectByKey). ApplyCurrentValues sounds sort of like what I want, but it only copies scalar values and doesn't seem intended to update the object graph in the ObjectContext/ObjectSet. Because of my particular use case I don't care about concurrency right now.
public void AddOrUpdate(BO biz)
{
object obj;
EntityKey ek = Ctx.CreateEntityKey(mySetName, biz);
bool found = Ctx.TryGetObjectByKey(ek, out obj);
if (found)
{
// How do I do what this method name implies? Biz is a parent with children.
mySet.TellTheSetToUpdateThisObject(biz);
}
else
{
mySet.AddObject(biz);
}
Ctx.DetectChanges();
}