2 回保存する必要があるエンティティがあります。1回目はIDの設定です。この ID は署名を計算するために入力する必要があり、その署名はエンティティに保存されます。
次のコードを参照してください。
var newEntity = new MyEntity
{
\\ set values
};
using (var db = MyContainer.CreateContainer())
{
db.MyEntity.Add(newEntity);
// Call SaveChanges() to set the ID.
db.SaveChanges();
// I need to do some calculation on the entity
myEntity.Signature = CalculateSignature(myEntity);
db.SaveChanges(); // <--- This causes the exception
}
このコードにより、InvalidOperationException が発生します。つまり、The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects.
わかりません。同じエンティティを 2 回保存することはできませんか? これを達成するにはどうすればよいですか?