I have a table of Persons and a table of Things, where each Thing is owned by a Person and each Person has a FavoriteThing.
Persons
PersonID int <PK>
FavoriteThingID int <FK>
Things
ThingID int <PK>
PersonID int <FK>
I would like to be able to add a Person and his/her favorite Thing, as well as setting that Thing's PersonID to the new Person, in a single transaction, without having DTC promote the transaction to distributed. Wrapping the operation in a TransactionScope() and manually managing the Entity connection does not appear to work:
ThingEntities ent = new ThingEntities();
using (TransactionScope scope = new TransactionScope())
{
ent.Connection.Open();
Thing t = ent.CreateObject<Thing>();
ent.Things.AddObject(t);
ent.SaveChanges(false);
Person p = ent.CreateObject<Person>();
t.Person = p;
ent.Persons.AddObject(p);
p.FavoriteThing = t;
ent.SaveChanges(false);
scope.Complete();
ent.AcceptAllChanges();
ent.Connection.Close();
}
This results in a "Unable to determine a valid ordering for dependent operations." exception on the second SaveChanges() call.
Is there a simple way to do this?
Thanks