1 year ago
#334916
Sebastian
Exclude AUTO_INCREMENT column value while using Entity framework Clone SetValues methods
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);
mysql
entity-framework
entity-framework-6
auto-increment
pomelo-entityframeworkcore-mysql
0 Answers
Your Answer