I am trying to insert a record into my database based on an existing record which functions as template using Entity Framework.
The flow right now is:
var newModel = organization.Models.Where(m => m.IsTemplate == true).FirstOrDefault();
newModel.Name = "Something New";
newModel.IsTemplate = false;
organization.Models.Add(newModel);
_organizationRepo.SaveChanges();
Note that organization.Models
is read from the repository beforehand.
My problem is that instead of creating a dublicate record with the new name and no tempalte flag, the template record is altered.
Criteria:
- I want to create a new record which is a copy of an existing record
- I want to alter the new record
- I do NOT want to alter the existing record
I would assume Entity Framework would interpret the organization.Models.Add(newModel)
statement as insert new record but it does not.