Reputation:
Working database and Achive Database. I need to move unwanted entries (Isdeleted equal 1) into a archive database. I am having this error message.
System.InvalidOperationException: 'An entity object cannot be referenced by multiple instances of IEntityChangeTracker.'
Any suggestion on how and what approach i can do.
In My Model class
public partial class log
{
public int id { get; set; }
public int logID { get; set; }
public Nullable<int> isDeleted { get; set; }
public virtual logCategory logCategory { get; set; }
}
In my actual database
Archive database Consist of: id, logID, isDeleted, logCategory
Working database Consist of: logId, isDeleted, logCategory
In my Controller
public class ProgrammingController : Controller
{
dbArchive2Entities _Archivecontext = new dbArchive2Entities();
dementiaDatabaseEntities _context = new dementiaDatabaseEntities();
public ActionResult index()
{
testing();
return View();
}
public void testing()
{
List<log> alldelLog = _context.logs.Where(x => x.isDeleted == 1).ToList();
_Archivecontext.logs.Add(alldelLog[0]);
_Archivecontext.SaveChanges();
}
}
Upvotes: 0
Views: 112
Reputation: 109261
The line...
List<log> alldelLog = _context.logs.Where(x => x.isDeleted == 1).ToList();
...attaches log
s to _context
's change tracker as Entity Framework proxies. These proxies have a reference to the context by which they were created.
You can fix this by preventing _context
from tracking the logs:
List<log> alldelLog = _context.logs
.AsNoTracking()
.Where(x => x.isDeleted == 1).ToList();
Upvotes: 1