2011-12-15 10 views
0

エンティティオブジェクトを複製/コピーしてDBに挿入しようとしています。私はEntity Frameworkを使用しています。エンティティオブジェクト:クローニングと挿入

私はいくつかの同様の質問がここと他のフォーラムで見つかりました。正確な問題はありますが、解決策のどれも私のために働いていませんでした。

Application orginalApp = new Application().GetById(origAppId); 
Application clonedApp = orginalApp.Clone<Application>(); //uses DataContractSerializer 
DataBaseContext.Current.Detach(orginalApp); // Current is a property which returns Database context which is stored in httpcontext.current.items 

clonedApp.EntityKey = null; // tried with and without this 

clonedApp.Application_Id = 0; // tried with and without this. This is the primary key 
clonedApp.Application_Name += " (clone)"; 

clonedApp.Create(); //The usual Addobject and SaveChange() 

これは、次のエラーをスロー:

An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.

Iは、オブジェクト状態マネージャ内のオブジェクトのexistanceを確認しようとしました。

DataBaseContext.Current.ObjectStateManager.GetObjectStateEntry(clonedApp) 

それは私にNullを与えます。

*けれども、私は、解決策を見つけるcouldntのリフレクションを使用して要件の回避策になった。*

+0

私はあなたが少なくともこれについてどのフレームワークを使用しているかを明確にする必要があると思います。 –

+0

よろしくお願いいたします。ごめんなさい。質問が編集されます。私はエンティティのフレームワークを使用しています – eMan

+0

私は反射を使用してクローニングを実装することでこれを解決しました – eMan

答えて

0

このエラーは、あなたがあなたの古いために行ったように、あなたの新しいオブジェクトに同じ主キーを持っていると言います1つ、行をクローンしている場合は、クローンされた行の新しいPKが必要になります

関連する問題