私のアプリケーションでは、エンティティの部分グラフを取得する必要があります。
これらのエンティティはユーザーによって処理され、後で保存する必要があります。
したがって、戻るコンテキストと保存するコンテキストは異なりますが、グラフ全体の変更を追跡する必要があります。
私の知る限り、STE は現在非推奨ですが
、このシナリオでどのタイプのエンティティを選択すればよいかわかりません。誰でも説明できますか?
1 に答える
1
1つ下を試すことができます。
切断されたエンティティの挿入
Insert<>
これは、切断されたエンティティを挿入できる汎用バージョンです。
public TEntity Insert<TEntity>(TEntity entity)
where TEntity : EntityObject
{
AddTo<TEntity>(entity);
this.SaveChanges(true);
// Without this, attaching new entity of same type in same context fails.
this.Detach(entity);
return entity;
}
切断された子エンティティの挿入
子エンティティを挿入する一般的な原則は次のとおりです。
まず、親エンティティをコンテキストにアタッチする必要があります。
次に、親と子の間のマッピングを設定する必要があります (まだマッピングを設定することはできません!)。
そして、SaveChanges を呼び出す必要があります。
コードは次のとおりです。
public TEntity Insert<TParent, TEntity>(
TParent parent,
Action<TParent, TEntity> addChildToParent,
TEntity entity)
where TEntity : EntityObject
where TParent : EntityObject
{
AddTo<TParent, TEntity>(parent, addChildToParent, entity);
this.SaveChanges();
this.AcceptAllChanges();
// Without this, consequtive insert using same parent in same context fails.
this.Detach(parent);
// Without this, attaching new entity of same type in same context fails.
this.Detach(entity);
return entity;
}
private void AddTo<TParent, TEntity>(TParent parent,
Action<TParent, TEntity> addChildToParent,
TEntity entity)
where TEntity : EntityObject
where TParent : EntityObject
{
Attach<TParent>(parent);
addChildToParent(parent, entity);
}
完全に切断された状態で動作する Entity Frameworkから詳細を取得できます。
これがお役に立てば幸いです。
于 2012-12-29T12:22:18.633 に答える