私はコードを最初に使用しています。私は毎日sourcedbからtargetdbにテーブル行をコピーするプロセスを設定しています。また、私は両方の主キーの値を維持する必要があります。 (これは必須です)つまり、両方のdbは、指定された行に対して同じ主キーを持つ必要があります。オブジェクト・タイプ 'MyProject.Model.Applications'のプロキシー・タイプが既に生成されています
同じクラスを参照して2つの異なるコンテキストを作成しました。両方のコンテキストはそのままです。私はsourcedbからオブジェクトのリストにすべての行を取得し、targetdbにその範囲を挿入するために別のコンテキストに渡しています。しかし、そうしている間、私は 'としてエラーを取得しています。既に、オブジェクト層タイプ' MyProject.Model.Applications 'のプロキシタイプが生成されています。これは、AppDomainで2つ以上の異なるモデルによって同じオブジェクトレイヤータイプがマップされている場合に発生します。
他のリンクを確認しました。しかし、これまでのところ何も働いていません。私もis it possible to share POCO object between two DbContext?をチェックしました。続き
は
using (var scope = new TransactionScope(TransactionScopeOption.RequiresNew, TimeSpan.FromSeconds(6000)))
{
using (var dbContext = new SourceDbContext())
{
DateTime dateToBeCompared = DateTime.UtcNow.Date.AddMonths(-11);
dbContext.Configuration.LazyLoadingEnabled = false;
dbContext.Configuration.AutoDetectChangesEnabled = false;
//get data from application related tables.
var applications = dbContext.Applications.AsNoTracking().Where(a => a.UpdatedOn <= dateToBeCompared)
.ToList();
using (var connection1 = new System.Data.SqlClient.SqlConnection("TargetDbConnectionString"))
{
connection1.Open();
using (var targetDbContext = new TargetDbContext(connection1, true))
using (TransactionScope tsSuppressed = new TransactionScope(TransactionScopeOption.Suppress))
{
targetDbContext.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].[Applications] ON");
}
try
{
targetDbContext.Applications.AddRange(applications);
targetDbContext.SaveChanges();
}
catch (Exception ex)
{
throw;
}
using (TransactionScope tsSuppressed = new TransactionScope(TransactionScopeOption.Suppress))
{
targetDbContext.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].[Applications] OFF");
}
}
connection1.Close();
}
scope.Complete();
}
はまた、いくつかの外部キー制約があり、いくつかの擬似コードです。しかし、そこには何があっても、両方の状況に共通しています。
ありがとうIvan。よく働く!!! – Pavvy