間でエンティティをコピーするEntity Frameworkのを使用して、私は2つのコンテキストを作成しています。4.同じスキーマを持つ2つの別々のデータベースを持つデータベース
をEntity Frameworkのを使用して別のデータベースからのエンティティ(レコード)をコピーする必要がありますが、私は2番目のコンテキストにあるエンティティを追加するとき、私は次のエラーを取得しています:
An entity object cannot be referenced by multiple instances of IEntityChangeTracker.
私はDetachメソッドを使用している場合、私はそのエラーを回避することができます知っているが、その場合には関連企業が失われます!
サンプルコード:
var cx = new MyEntities();
//eager load related tables
var allEntities = from x in cx.Reservation.Include("Detail.MoreDetail")
select x;
// new instance of context but connected to a second database
var cx2 = new MyEntities(new ConnectionString...);
foreach (var e in allEntities)
{
//cx.Detach(reservation); // can't detach, or related entities will be lost
cx2.AddToReservation(reservation); // error happens here!
cx2.SaveChanges();
}
どのように私は、このような操作を行うことができますか? また、関連エンティティを失うことなくエンティティを切り離すにはどうすればよいですか?
かなりくそ便利な、おかげで – fernandoespinosa