0

I am using below code to clone records from one table to another using Entity Framework

List<MasterTableRecord> _records = context.MasterTableRecords
                                            .Where(c => ......).ToList();
_records?.ForEach(record =>
{
    var _recordValues = context.Entry(record).CurrentValues.Clone();

    var recordM = new HistoryMaster();
    context.Entry(recordM).CurrentValues.SetValues(_recordValues);
    context.HistoryMaster.Add(recordM);

    context.MasterTableRecords.Remove(record);
});

At a later time the application might re insert the record back to the Master Table from History and at that point the auto increment ID is causing troubles. Over the period of time some other records will consume the auto increment id on the master tabe and while saving the changes it will cause Duplicate entry 'xxx' for key 'xx.PRIMARY' will occur

Example: ID : 10 moved to history and after 2 days if we move record from history back to master , there are chances that some other records will be already inserted to master with same ID 10 .

So while moving records back from history to master , how can we tell the system to assume new available auto increment ID

var recordM = new HistoryMaster();
context.Entry(recordM).CurrentValues.SetValues(_recordValues);
Sebastian
  • 4,625
  • 17
  • 76
  • 145
  • 1
    SQL Server <> MySQL. Only tag the RDBMS you are *really* using. `AUTO_INCREMENT`is MySQL terminology, so presumably you are using that (SQL Server uses `IDENTITY`) – Thom A Mar 25 '22 at 10:56
  • I am using Mysql and corrected the tags accrordingly – Sebastian Mar 25 '22 at 11:00

0 Answers0