Is there a best practice for copying a copy of an object, making any changes to it based on user input, and then reinstalling it in the database?
Some other Stackoverflow threads mentioned that EF will handle inserting new objects for you, even if the same primary key exists in the database, but I'm not quite sure how this works with EF Core. Whenever I try to copy an object, I get an error
Cannot insert explicit value for identity column in table when IDENTITY_INSERT is set to OFF
Basically, I just need a clean way to copy an object, make some changes to it based on user input, and then paste that copy back into the database and set up Id auto-increment correctly. Is there any best practice or an easy way to do this without having to manually set the properties to null or empty?
EDIT: sample code for retrieving an object from a database:
public Incident GetIncidentByIdForCloning(int id)
{
try
{
return _context.Incident.Single(i => i.IncidentId == id);
}
catch
{
return null;
}
}
Code after retrieving the object (since some fields are automatically generated, for example, RowVersion, this is a timestamp):
public IActionResult Clone([FromBody]Incident Incident)
{
var incidentToCopy = _incidentService.IncidentRepository.GetIncidentByIdForCloning(Incident.IncidentId);
incidentToCopy.IncidentTrackingRefId = _incidentService.IncidentRepository.GetNextIdForIncidentCategoryAndType(
Incident.IncidentCategoryLookupTableId, Incident.IncidentTypeLookupTableId).GetValueOrDefault(0);
incidentToCopy.RowVersion = null;
incidentToCopy.IncidentId = 0;
incidentToCopy.IncidentCategoryLookupTableId = Incident.IncidentCategoryLookupTableId;
incidentToCopy.IncidentTypeLookupTableId = Incident.IncidentTypeLookupTableId;
var newIncident = _incidentService.IncidentRepository.CreateIncident(incidentToCopy);
...
I understand that I can just make a completely new object and do left copy, but it seems terribly inefficient, and I want to know if EF Core offers the best solutions.