0
(注:私はthisを参照してください、それは私がやりたい正確に何ではありません。)EF4 - トランザクション内でアイデンティティを抽出し、後続のSaveChanges()を実行します。デタッチまたは複数のコンテキスト?
私は次の操作を行う必要がある要件があります。
- を新しい
Foo
エンティティを作成します - Fooエンティティが作成されると、新しい
FooAudit
エンティティを作成し、テキスト説明の新しいFoo
エンティティの新しい識別値を使用します。FooAudit
はFooとの関係がないため、IDはFoo
の作成から手動で取得する必要があります。 - トランザクションをコミットしない場合、
EntityState
のFoo
レコード(Added
)を保持して、これらの処理をすべて実行するか、またはまったく実行しないでください。
他のコンテキストを使用していますか?
using (var fooContext = GetContext()) { fooContext.Foos.AddObject(new Foo()); using (var transaction = new TransactionScope()) { // Save Foo, while maintaining the Changed EntityState until AcceptAllChanges fooContext.SaveChanges(SaveOptions.DetectChangesBeforeSave); using (var auditContext = GetContext()) { // Save FooAudit, maintaining the Changed EntityState until AcceptAllChanges auditContext.FooAudits.AddObject(new FooAudit()); auditContext.SaveChanges(SaveOptions.DetectChangesBeforeSave); transaction.Complete(); fooContext.AcceptAllChanges(); auditContext.AcceptAllChanges(); } } }
もっと良い方法がありますか?