Copy an Entity With Navigation Property in C# I have a table named Asset and each Asset has schedules. I create a method like this for copying an entity:

    public bool Copy(CopyAssetViewModel copyAssetViewModel,out string message)
    {
        var source = _equipments.AsNoTracking().FirstOrDefault(row =&gt; row.Id == copyAssetViewModel.AssetId);
        if (source == null) return false;
        int count = source.Schedules.Count;
        Asset newAsset = new Asset();
        Guid tempId = newAsset.Id;
        newAsset = source;
        newAsset.Id = tempId;
        newAsset.AssetCode = copyAssetViewModel.AssetCode;
        if (copyAssetViewModel.CopySchedules)
        {
            newAsset.Schedules = source.Schedules.ToList();
            newAsset.Schedules.ToList().ForEach(row =&gt;
            {
                Schedule newSchedule = new Schedule();
                Guid scheduleId = newSchedule.Id;
                newSchedule = row;
                newSchedule.Id = scheduleId;
                newSchedule.AssetId = tempId;
            });
        }
        else
            newAsset.Schedules.Clear();
        _uow.MarkAsAdded(newAsset, _userManager.GetCurrentUserId());
        _uow.SaveChanges();
        return true;
    }


All entities inherit BaseEntity and I have a constructor for creating a new Id like this:

 public BaseEntity()
    {
        Id = SequentialGuidGenerator.NewSequentialGuid();
        CreatedOn = DateTime.Now;
    }


But when I use this code:

newAsset.Schedules = source.Schedules.ToList();


Id and createdOn are the same. I've gotten an error for a duplicate key. For solve this I used these lines:

 Schedule newSchedule = new Schedule();
                Guid scheduleId = newSchedule.Id;
                newSchedule = row;
                newSchedule.Id = scheduleId;


And I have another field that needs to update like: CreatedByUserId.

I think my code is very poor. Can everyone give me an idea to improve it?

copyAssetViewModel:

public class CopyAssetViewModel
{
    public Guid AssetId        { get; set; }
    public bool CopySchedules  { get; set; }
    public int AssetCode       { get; set; }
}
It appears that _equipments.AsNoTracking().FirstOrDefault gets some objects that have a unique ID  

When you _uow.MarkAsAdded(newAsset, _userManager.GetCurrentUserId()); that collection requires unique ID. You go through round about way to to create a new ID so that you don't get a conflict.  

Same thing for both Asset ID and Schedule ID.  

All this is doing is assign a new unique ID.  

Schedule newSchedule = new Schedule();
Guid scheduleId = newSchedule.Id;
newSchedule = row;
newSchedule.Id = scheduleId; 


newSchedule is a reference to row so  newSchedule.Id = scheduleId; is just assigning a new ID to row  

It someone is reading that different please comment.  

It appears you changing code to get rid of error messages without fully understanding the code and objectives.