Not in SQL but in C# using Entity Framework: Is there a simple way to copy a source record to another table? The column definitions are identical however the target table has no indices, keys or constraints. I have this code and the one line I am having trouble with is marked:
private void HandleAutos()
{
// convert to List<auto>...
List<Model.Auto> imports = ConvertAuto();
// get all DB records in List<auto>...
List<Model.Auto> current = _dbFeed.Autoes.Where(a => a.ClientID == _targetClientID && a.Active == true).ToList();
// isolate all Inserts, Updates and Deletes...
var existing = imports.Intersect(current, new AutoIsIn()).ToList(); // should be all autos with matching VIN & SKU //
var updates = existing.Intersect(current, new AutoHasChanged()).ToList(); // should be a subset of changed resords //
var inserts = imports.Except(current, new AutoIsIn()).ToList(); // should be all the imports not in the DB //
var deletes = current.Except(imports, new AutoIsIn()).ToList(); // should be all the DB records not in imports //
// THIS IS THE BAD LINE //
deletes.ForEach(a => _dbFeed.AddToAutoArchives(a)); // <=======
// -------------------- //
deletes.ForEach(a => _dbFeed.DeleteObject(a));
updates.ForEach(a => _dbFeed.ApplyCurrentValues("Autoes", a));
inserts.ForEach(a => _dbFeed.AddToAutoes(a));
_dbFeed.SaveChanges();
}
I know I can loop through my source records, create an Archive record, copy each property over and finally add the new entity to the ArchiveAutos collection and save it to the DB. Before I went down that path and wrote a method to copy a record from an Auto entity to an AutoArchive entity I though I would check to see if there is already some awesome sweetness already baked in.
Do you know if there is any?
TIA
Here is the solution I came up with:
Ugly as hell but it works. 🙁