I've a situation where I needed to duplicate/clone an entity record resulting in a new record in the database?
I've tried the following code but getting the error as
Collection was modified; enumeration operation may not execute.
This is the code I've tried
var _dbWalkInn = _db.StudentWalkInns
.Where(r => r.Id == regId)
.FirstOrDefault();
if (_dbWalkInn != null)
{
var _newWalkInn = new StudentWalkInn();
var propInfo = _dbWalkInn.GetType().GetProperties();
foreach (var item in propInfo)
{
_newWalkInn.GetType().GetProperty(item.Name).SetValue(_newWalkInn, item.GetValue(_dbWalkInn, null), null);
}
_newWalkInn.Id = 0;
_db.StudentWalkInns.Add(_newWalkInn);
_db.SaveChanges();
}
How can I duplicate/clone the entity ??